My array of objects contains objects of different kinds, meaning they can have very different properties. However they all share an id
property.
When my app's user wants to update one of these objects, I need to find that object in the array based on the id
and update all its properties, probably using Object.assign()
.
I know how to find and return the object I want based on the id
, but not how to update it as well. Any idea?
let myArray = [
{
id: 1,
name: 'john',
age: '22',
position: 'developer'
},
{
id: 2,
name: 'james',
age: '31',
position: 'designer'
},
{
id: 1,
name: 'david',
age: '45',
position: 'teacher'
}
]
let updateForJames = {
age: '38',
position: 'project manager'
}
let JamesId = 2
Object.assign(myArray, {
// ...
})