constructor (props) {
super(props)
this.state = { items: this.props.items }
// items props is: [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}]
}
onClick () {
const myArray = this.state.items
const ids = ['45', '73']
ids.forEach((id, index) => {
myArray.find(x => x.id === id).foo = index
})
}
I need to change foo value to index value. So the result should look like
myArray = [{'id':'73','foo': 1},{'id':'45','foo': 0}]
I think with this, I do get the current value, but the syntax is wrong to change its value:
myArray.find(x => x.id === '45').foo = 'new'
I do get the error Uncaught TypeError: Cannot assign to read only property 'foo' of object '#<Object>'
this.state = { items: this.props.items }because I'll modify items as shown in the post. But with that I do get the errorUncaught TypeError: Cannot assign to read only property 'foo' of object '#<Object>'- also for your code.