I have a method that replaces one object in an array with another one. It will search by ID and change all records that match by the designated ID.
I would like to change this to a generic, reusable method that can operate in the same way for any object type I pass in. (EG: car.id
, car.door.id
, car.trunkMonkey.id
, etc.)
Is there a way for me to pass the "element.car.id" path as a variable into this method?
updateArrayObject(item: any, data: any[]) {
// Parse all objects in the array, looking for matches by ID
data.forEach(element => {
if (element.car.id === item.id) { // <-- Can I pass "element.car.id" into the method somehow?
// Item ID matches. Replace the item.
element.car = item;
}
});
}