我想弄清楚如何在ES6中做到这一点......
我有这个对象数组..
const originalData=[
{"investor": "Sue", "value": 5, "investment": "stocks"},
{"investor": "Rob", "value": 15, "investment": "options"},
{"investor": "Sue", "value": 25, "investment": "savings"},
{"investor": "Rob", "value": 15, "investment": "savings"},
{"investor": "Sue", "value": 2, "investment": "stocks"},
{"investor": "Liz", "value": 85, "investment": "options"},
{"investor": "Liz", "value": 16, "investment": "options"}
];
Run Code Online (Sandbox Code Playgroud)
..和这个新的对象数组,我想添加每个人的投资类型(股票,期权,储蓄)的总价值..
const newData = [
{"investor":"Sue", "stocks": 0, "options": 0, "savings": 0},
{"investor":"Rob", "stocks": 0, "options": 0, "savings": 0},
{"investor":"Liz", "stocks": 0, "options": 0, "savings": 0}
];
Run Code Online (Sandbox Code Playgroud)
我循环遍历originalData并在let中保存"当前对象"的每个属性.
for (let obj of originalData) …Run Code Online (Sandbox Code Playgroud)