I have array of objects and the format is like this
addedOpaqueMaterials = [{
conductivity: 1
density: 1
id: "1"
......
......
},
{
conductivity: 2
density: 1
id: "2",
......
......
}]
and from the react state i am getting updated object with the id= 2
like as this below,
{
conductivity: 4
density: 23
id: "2",
......
......
}
and i am looking to update the object in main array with these values, what could be the best way to update, below is the code related to that.
const handleDrawerSubmit = values => {
const addedOpaqueMaterials = formValues.constructionSet?.opaqueMaterials; // array of objects
const updatedMaterial = addedOpaqueMaterials.find(i => i.id === values?.opaqueMaterial?.id);
// values?.opaqueMaterial is having updated object
// Object.assign(updatedMaterial, values.opaqueMaterial); getting an error
};
Now I would like to merge values.Opaquematerial
object into addedOpaqueMaterials
which is array of objects, what could be the best way to achieve this?
Many thanks