簡體   English   中英

合並兩個對象數組並覆蓋鍵值

[英]Merge two arrays of objects with override on key value

我正在嘗試合並來自對象數組中的 json 數據。 我正在使用這里的下划線解決方案, 根據 javascript 中的鍵值合並兩個 json 對象,但事實證明它不會覆蓋我現在也需要做的現有項目。

結果應該是數組 1 的所有項目以相同的順序,被數組 2 覆蓋,其中 id = id。 數組 2 中不存在於數組 1 中的項目應被推送到結果的末尾。

第一個數組:

[
 {id: 8, category: "A"}
 {id: 2, category: "D"}
 {id: 5, category: "C"}
 {id: 9, category: "B"}
]

第二個數組:

[
 {id: 1, category: "X"}
 {id: 2, category: "Y"}
]

預期結果:

[
 {id: 8, category: "A"}
 {id: 2, category: "Y"}
 {id: 5, category: "C"}
 {id: 9, category: "B"}
 {id: 1, category: "X"}
]

使用filterfindconcat

鑒於

var arr1 = [
 {id: 8, category: "A"},
 {id: 2, category: "D"},
 {id: 5, category: "C"},
 {id: 9, category: "B"}
];
var arr2 = [
 {id: 12, category: "X"},
 {id: 2, category: "Y"}
];

如果順序不重要

var output = arr2.concat( 
        arr1.filter( s => 
            !arr2.find( t => t.id == s.id ) 
        )//end filter 
);//end concat

演示

 var arr1 = [{ id: 8, category: "A" }, { id: 2, category: "D" }, { id: 5, category: "C" }, { id: 9, category: "B" } ]; var arr2 = [{ id: 12, category: "X" }, { id: 2, category: "Y" } ]; var output = arr2.concat( arr1.filter(s => !arr2.find(t => t.id == s.id) ) //end filter ); //end concat console.log(output);

如果訂單很重要

var output = arr1.map( 
       s => arr2.find( 
           t => t.id == s.id ) || s 
).concat( //end map of arr1
      arr2.filter( 
           s => !arr1.find( t => t.id == s.id ) 
      ) //end filter
);//end concat

演示

 var arr1 = [{ id: 8, category: "A" }, { id: 2, category: "D" }, { id: 5, category: "C" }, { id: 9, category: "B" } ]; var arr2 = [{ id: 12, category: "X" }, { id: 2, category: "Y" } ]; var output = arr1.map( s => arr2.find( t => t.id == s.id) || s ).concat( //end map of arr1 arr2.filter( s => !arr1.find(t => t.id == s.id) ) //end filter ); //end concat console.log(output);

您可以使用Map作為閉包並為此id存儲結果數組的索引。

 var first = [{ id: 8, category: "A" }, { id: 2, category: "D" }, { id: 5, category: "C" }, { id: 9, category: "B" }], second = [{ id: 12, category: "X" }, { id: 2, category: "Y" }], result = [first, second].reduce((m => (r, a) => { a.forEach(o => { if (m.has(o.id)) { r[m.get(o.id)] = o; return; } m.set(o.id, r.push(o) - 1); }); return r; })(new Map), []); console.log(result);

您可以在secondArray上設置一個循環,並根據idfirstArray對象檢查每個具有id值的對象。 如果找到匹配項,則只需替換對象,否則推送對象:

 var firstArray = [ {id: 8, category: "A"}, {id: 2, category: "D"}, {id: 5, category: "C"}, {id: 9, category: "B"} ]; var secondArray = [ {id: 12, category: "X"}, {id: 2, category: "Y"} ]; secondArray.forEach((obj)=>{ var match = false; for(var i=0; i<firstArray.length; i++){ if(firstArray[i].id === obj.id){ match = true; firstArray[i] = obj; break; } } if(!match){ firstArray.push(obj); } }); console.log(firstArray);

您可以使用forEach遍歷second數組。 對於第一個數組中具有相同 id 的每個對象,更新category否則推入新數組。

 const first = [{id: 8, category: "A"},{id: 2, category: "D"},{id: 5, category: "C"},{id: 9, category: "B"}], second = [{id: 12, category: "X"},{id: 2, category: "Y"}], merged = [...first]; second.forEach(o => { let obj = first.find(({id,category}) => id === o.id); obj ? obj.category = o.category : merged.push({...o}); }); console.log(merged);

我認為減少更好

first.reduce((res, item) => res.filter(i => i.id !== item.id).concat(item), second);

使用下划線我設法想出了這個答案我自己的問題。 這可能不是最有效的

const newarr = _.map(arr1, obj1 => {
  const r = _.find(arr2, obj2 => {
    return obj1[match] === obj2[match]
  })
  if (typeof r === 'undefined') {
    return obj1
  } else {
    return r
  }
})
_.each(arr2, obj => {
  if (_.indexOf(arr1, _.findWhere(arr1, {id: obj.id})) === -1) { 
   newarr.push(obj) 
  }
})

這應該有效:

const newArr = second.reduce((res, item) => res.filter(i => i.id !== item.id).concat(item), first);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM