I have an array of person objects and I want to update one of object in place.
persons: [{
id: '1',
name: 'John',
age: 12
}, {
id: '2',
name: 'Tom',
age: 13
}, {
id: '3',
name: 'David',
age: 14
}]
The function I have is:
function updatePersonsWith(id, propName, value) {
this.persons.???
}
The arguments passed are id of the person I want to update, propName is the properties of person object, can be id, name or age, value is the value I want to replace with.
I want to find an object by it's id and update only this object of the array.
updatePersonsWith(2, age, 16)
The result would be:
persons: [{
id: '1',
name: 'John',
age: 12
}, {
id: '2',
name: 'Tom',
age: 16
}, {
id: '3',
name: 'David',
age: 14
}]
Could be ES6 or using lodash.