How do we create specific fields of object in the array?
Here is my state data that I want to partially update.
state.items = [
{id:0, left: 100, right: 100, classified: true},
{id:1, left: 200, right: 300, classified: false},
]
And this is expected result after setState.
// Edit left, right, classified with given values,
// where the object's id is equal to 1.
state.items = [
{id:0, left: 100, right: 100, classified: true},
{id:1, left: 300, right: 200, classified: true},
]
Here is code inside of function (Problem)
//console.log(id) <- prints 1. We are modifying the second object in the array with id 1.
// iterate items array
let { items } = this.state;
for (var index in items) {
if(items[index].id === id) {
// copy the whole object matching id is 1.
var deep = _.cloneDeep(tag);
//Assign new values (newLeft: 300, newRight:200, newCalssified: true)
deep.left = newLeft
deep.right = newRight
deep.classified = newClassified
this.setState({
// HERE IS THE QUESTION
taggedClothes // how do we modify the object with id 1??
})
}
}
THANKS!!