I have the following example array of objects
[
{ key : '11', value : '1100', $$hashKey : '00X' },
{ key : '22', value : '2200', $$hashKey : '018' }
];
that I would like to only have individual objects like:
{ key : '11', value : '1100', $$hashKey : '00X' },
{ key : '22', value : '2200', $$hashKey : '018' }
and store them in useState like so
array.map((object)=>{
setObjects({...object})
})
but when do
console log(objects)
it only outputs one object not all of them
You are overriding your object after each iteration. One way is to make a new object if you are sure the key will be unique then use this one.
array.map((object)=>{
setObjects((prevState) => ({...prevState, [object.key]: object }) )
})
It will produce the output like this.
{
11 : { key : '11', value : '1100', $$hashKey : '00X' },
12 : { key : '22', value : '2200', $$hashKey : '018' }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.