【发布时间】:2017-11-09 16:24:13
【问题描述】:
我有一种方法可以将数组中的一个对象替换为另一个对象。它将按 ID 搜索并更改所有与指定 ID 匹配的记录。
我想将其更改为通用的、可重用的方法,该方法可以以相同的方式对我传入的任何对象类型进行操作。(例如:car.id、car.door.id、car.trunkMonkey.id 等)
有没有办法让我将“element.car.id”路径作为变量传递给这个方法?
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;
}
});
}
【问题讨论】:
标签: javascript arrays angular typescript