Add attribute to a json

Asked

Viewed 240 times

0

How do I add an extra attribute to all elements of a JSON that I receive, to then display on the screen?

When I try to make one map in the JSON that I receive from the request and add the item in the not right state because of the await async, then it only adds 1.

I need to add, in a array of objects I receive on JSON a Qtd attribute on each object, when using the Spread operator ([...dados,{qtd:0}]) he does not at all array.

Example:

const montaAlgoMais = async () => {

    try {
  
        const res = await axios.get(url)
            .then(({ data }) => {
                    data.map((item) => {
                        return setAlgoMais([...algoMais,{...item,qtd:0}])
                    })
            })
            .catch(err => console.log(err));
    } catch (e) {
        console.log(e)
    }

}

1 answer

0


To pass an object with more attributes, just copy the data with the technique Spread Operator as in the example:

const data = {
  'id': 1, 
  'name': 'example 1'
}

const finalData = { ... data, source: new Date() };

console.log(finalData);

this command also works for array of example objects:

const datas = [
  {
    'id': 1, 
    'name': 'example 1'
  }, 
  {
    'id': 2, 
    'name': 'example 2'
  }
]

const finalData = datas.map(data => {
  return {... data, source: new Date()};
});

console.log(finalData);

In your solution it’s all wrong, you mixed it up and the right thing is:

const montaAlgoMais = async () => {
    try {  
        const res = await axios.get(url);
        const newData = res.data.map(item => { 
            return { ...item, qtd: 0};
        });
        setAlgoMais(newData);
    } catch (e) {
        console.log(e)
    }
}
  • how do I do this, getting the information of a request in Xios? why do I add with operator spread, and it seems that it does only once

  • someone can help me???

  • @Matheussaleh as this one comes json? I gave you all the cards which the difficulty

  • return (setData([...data])) first problem this is not necessary!

  • If you get a array and need to create one more key within each position need to use map as I showed you in the second example. @Matheussaleh

  • It may be that I expressed myself badly, but I reported on the problem. I get the information from a json. I make a request with await in Xios and get a json. And if I put it with the spread operator, it works, but only if it’s in the whole json. I want to add a field to all objects, only when I use a foreach or map, in the data I receive, it only returns 1 object of the 20 that comes in json.

  • then you did something wrong, pass the question what you really did there is more comfortable opining! In general if you are missing something, then in the question ask what you are doing and what you expect to happen !!! I am waiting for you.

  • added the question what I did

  • The way it is there, it returns me an array with 1 object , and if I update the page , increases, but always brings the same object

  • I did the editing and everything you had done was wrong, Watching the correct @Matheussaleh

  • Thanks for the help. Sorry for the lack of coherence of the questions, I don’t have much experience with React Native.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.