我有两个对象数组,我想对具有相同键(在本例中为 id)的对象求和,如果没有匹配键,则只需创建一个新的。如果我是,我很抱歉没有解释清楚,我对 JavaScript/Array/Object 很陌生......
var dataOne = [ { id:"1", total: 10, win: 5 }, { id:"2", total: 5, win: 1 }, { id:"3", total: 5, win: 2 } ]
Run Code Online (Sandbox Code Playgroud)
和
var dataTwo = [ { id:"1", total: 5, win: 2 }, { id:"2", total: 2, win: 3 }, { id:"5", total: 5, win: 4 } ]
Run Code Online (Sandbox Code Playgroud)
预期结果:
var combinedData = [ { id:"1", total: 15, win: 7 }, { id:"2", total: 7, win: 4 }, { id:"3", total: 5, …Run Code Online (Sandbox Code Playgroud)