【发布时间】:2017-04-15 06:36:50
【问题描述】:
如果我有一个对象汽车:
{
make: "Chevy",
model: "Sonic"
type: {
id: 1,
name: "Tiny"
}
}
我想操作对象来添加一个 typeId 属性:
{
make: "Chevy",
model: "Sonic"
type: {
id: 1,
name: "Tiny"
},
typeId: 1
}
假设您有一个包含这些对象的数组,您会使用哪个 rxjs 运算符?这是我要避免的:
someArray.map(item => ({
make: item.make,
model: item.model,
type: item.type,
typeId: item.type.id
}));
【问题讨论】:
-
如果你只是添加,为什么不
someArray.forEach(o => o.typeId = o.type.id) -
你到底想避免什么?
-
重写整个模型,因为我使用的模型比这辆车大得多。 ;-)
-
@adeneo 为什么不呢?因为我没有考虑 forEach。打得很好,先生。
-
@adeneo 随时提交作为答案。与此同时,我忍不住自嘲。
标签: javascript arrays object rxjs rxjs5