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)
}
}
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
– Matheus Saleh
someone can help me???
– Matheus Saleh
@Matheussaleh as this one comes
json
? I gave you all the cards which the difficulty– novic
return (setData([...data]))
first problem this is not necessary!– novic
If you get a
array
and need to create one more key within each position need to usemap
as I showed you in the second example. @Matheussaleh– novic
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.
– Matheus Saleh
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.
– novic
added the question what I did
– Matheus Saleh
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
– Matheus Saleh
I did the editing and everything you had done was wrong, Watching the correct @Matheussaleh
– novic
Thanks for the help. Sorry for the lack of coherence of the questions, I don’t have much experience with React Native.
– Matheus Saleh