If I have an object todo as part of your state, and that object contains arraylists, inside the lists there are objects, and inside these objects another arraylistItems. How do you update an object inside the array listItems with id:" poi098 "?
Demo here: https://stackblitz.com/edit/react-fjyb9g
class App extends Component {
constructor() {
super();
this.state = {
todo: {
id: "abc123",
name: "AAA",
lists: [
{
id: "def456",
desc: "description",
listItems: [
{
id: "ghj678",
title: "title1",
listItemsId: "88abf1"
},
{
id: "poi098",
title: "title2",
listItemsId: "2a49f25"
}
]
},
{
id: "zxc456",
desc: "description3",
listItems: [
{
id: "qyu678",
title: "title3",
listItemsId: "h60bf1"
},
{
id: "p8l098",
title: "title4",
listItemsId: "6yuf25"
}
]
}
]
}
}
}
addNew = () => {
const data = {
id: "poi098",
title: "title23333333333",
listItemsId: "2a49f25"
}
const todoCheck = this.state.todo['lists'].filter(obj => obj.id === "zxc456")
const todoCheckList = todoCheck.map((obj, i) => obj['listItems'])[0]
this.setState(prevState => ({
...prevState,
todo: {
...prevState.todo,
lists: {
...prevState.todo.lists,
listItems: {
...prevState.todo.lists.listItems,
listItems: todoCheckList
}
}
}
}))
}
render() {
console.log(this.state.todo)
return (
<div>
<Hello name={this.state.name} />
<p>
<button onClick={this.addNew}>Button</button>
</p>
</div>
);
}
}