I am using react, and I got a nested object like this:
const obj = useRef({
category1: [
{ title: { name1: 'value1' } },
{ title: { name2: 'value2' } }
],
category2: [
{ title: { name3: 'value3' } },
{ title: { name4: 'value4' } }
]
});
When I trying to update the category1 by adding a newTitle object in category1[index], the title object got replaced.
what I want:
const obj = useRef({
category1: [
{ title: { name1: 'value1' } },
{ title: { name2: 'value2' }, newTitle: { name5: 'value5' } }
],
category2: [
{ title: { name3: 'value3' } },
{ title: { name4: 'value4' } }
]
});
what I got:
const obj = useRef({
category1: [
{ title: { name1: 'value1' } },
{ newTitle: { name5: 'value5' } }
],
category2: [
{ title: { name3: 'value3' } },
{ title: { name4: 'value4' } }
]
});
I tried few different ways and none of it works.
const titleVar = 'newTitle';
const nameVar = 'name5';
const valueVar = 'value5';
obj.current[ctg][index][titleVar] = { [nameVar]: valueVar };
obj.current = {
...obj.current,
[ctg]: [
...obj.current[ctg].slice(0, index),
{
...obj.current[ctg][index],
[titleVar]: { [nameVar]: valueVar }
},
...obj.current[ctg].slice(index + 1)
]
};
const temp = {...obj.current[ctg][index],
[titleVar]: { [nameVar]: valueVar } };
obj.current = temp;
2
Answers
When updating the nested object, you need to make sure to preserve the existing properties of the object while adding new properties. you aren’t using spread operator properly. here is the snippet which will get you the result you desire. click on update Object button to update the obj.
I don’t know anything about React, but I got the results you expected with this Vanilla Javascript code, which looks a lot like one of the things you tried:
Which is to say: