let's say i have arrays with useState in React. In this arrays i have objects
const [myArray, setMyArray] = useState([
{ id: 1, name: "Butter", pieces: 4},
{ id: 2, name: "Bread", pieces: 2}
])
const [myArray2, setMyArray2] = useState([
{ id: 1, name: "Butter", pieces: 4},
{ id: 2, name: "Bread", pieces: 2}
])
on the UI i have the ability to move this items from myArray from a container to the next container where the Items of myArray2 are inside
when i move the 2 pieces of the butter from myArray to the Container where the Items of myArray2 are, it should filter for Butter in myAarray2 and if it found the butter it should update the pieces of the founded Butter in myArray2 with the 2 pieces that i moved so at the that at the end the array should like this
const [myArray, setMyArray] = useState([
{ id: 1, name: "Butter", pieces: 2},
{ id: 2, name: "Bread", pieces: 2}
])
const [myArray2, setMyArray2] = useState([
{ id: 1, name: "Butter", pieces: 6},
{ id: 2, name: "Bread", pieces: 2}
])
i only can't figure out how to make the filter function for finding the Butter in myArray2 and update it with 4 + 2 so that there are 6 pieces then
i know it should be like that:
const itemthathastobeupdated = myArray2.filter((item) => item.name == props.name);
would be great if someone can help me out here