Iron Brew
Iron Brew

Reputation: 21

How to update a nested array of objects in an object in react

Would appreciate any help on how to update the value property of any of the answer objects.

{
name: 'John', 
surname: 'Doe', 
answer: [{value: '' }, {value: ''}] 
}, 
{
name: 'Alice', 
surname: 'Malice', 
answer: [{value: '' }, {value: ''}] 
}
]

Upvotes: 0

Views: 69

Answers (1)

Nate Norris
Nate Norris

Reputation: 996

You'll just need to index into those arrays and set the values as needed. Here's a function that does just that:

const people = [...<my list of objects>...];

const updateValue = (name, answerIndex, newValue) => {
  const person = people.find((i) => i.name === name);
  person.answer[answerIndex].value = newValue;
}

Upvotes: 1

Related Questions