2016-11-30 43 views
0

如果我有一個對象添加與.MAP(屬性)

{ 
    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 
})); 
+3

地圖上如果您'只是加入,爲什麼不'someArray.forEach(o => o.typeId = o.type.id)' – adeneo

+0

你到底想要避免什麼? –

+0

重寫整個模型,因爲我使用的模型比這輛車大得多。 ;-) – wolfhoundjesse

回答

1

在每個對象數組,你可以使用forEach中添加屬性,你不必陣列

someArray.forEach(o => o.typeId = o.type.id)