2016-12-30 91 views
3

我需要通過公共ID合併多個json對象。我的問題是,我的對象有不同的密鑰ID。合併具有不同ID的多個對象數組

var object1 = [ 
    { "name":"apples" ,"w": 1, "x": 2 }, 
    { "name":"banana" ,"w": 1, "x": 2 }, 
    { "name":"cherry" ,"w": 1, "x": 2 }, 
]; 
var object2 = [ 
    { "type":"banana" ,"y": 3, "x": 4 }, 
    { "type":"cherry" ,"y": 3, "x": 4 }, 
]; 

我想獲得:

var object1 = [ 
    { "name":"apples" ,"w": 1, "x": 2 }, 
    { "name":"banana" ,"w": 1, "x": 4, "y": 3 }, 
    { "name":"cherry" ,"w": 1, "x": 4, "y": 3 }, 
]; 

我想使用相同的陣列[object1],而不是創建一個新的。 我創建了一個codepen here

+2

如果有什麼第二個'object2'將包含少見的水果,比如'{ 「類型」: 「石灰」, 「Y」:2, 「X」:4}'?在這種情況下應該是什麼結果? – RomanPerekhrest

+0

[合併2對象數組]的可能重複(http://stackoverflow.com/questions/7146217/merge-2-arrays-of-objects) –

回答

2

您可以使用reduce()創建新的數組和發現檢查具有相同名稱的對象是否存在於相同類型的object2對象中。

var object1 = [ 
 
    { "name":"apples" ,"w": 1, "x": 2 }, 
 
    { "name":"banana" ,"w": 1, "x": 2 }, 
 
    { "name":"cherry" ,"w": 1, "x": 2 }, 
 
]; 
 
var object2 = [ 
 
    { "type":"banana" ,"y": 3, "x": 4 }, 
 
    { "type":"cherry" ,"y": 3, "x": 4 }, 
 
]; 
 

 

 
var result = object1.reduce(function(r, e) { 
 
    var o = object2.find(a => e.name == a.type); 
 
    r.push(o ? Object.assign({}, e, {x: o.x, y: o.y}) : e); 
 
    return r; 
 
}, []) 
 

 
console.log(result)

+0

嘿,你的解決方案工作真棒,我現在的問題是,即不需要.find你知道如何克服嗎? –

+0

嘗試像這樣https://jsfiddle.net/Lg0wyt9u/1435/過濾器 –

2

遍歷object2和使用Array.prototype.find更新水果若發現 - 見演示如下:

var object1 = [{ "name":"apples" ,"w": 1, "x": 2 },{ "name":"banana" ,"w": 1, "x": 2 },{ "name":"cherry" ,"w": 1, "x": 2 }]; 
 
var object2 = [{ "type":"banana" ,"y": 3, "x": 4 },{"type":"cherry" ,"y": 3, "x": 4 }]; 
 

 
object2.forEach(function(e){ 
 
    var found = object1.find(function(k){ 
 
    return k.name === e.type; 
 
    }); 
 
    if(found) { 
 
    found.x = e.x; 
 
    found.y = e.y; 
 
    } 
 
}); 
 

 
console.log(object1);
.as-console-wrapper{top:0;max-height:100%!important;}

0

我犯了一個解決你的問題,你可以試試嗎?

var object1 = [ 
 
    { "name":"apples" ,"w": 1, "x": 2 }, 
 
    { "name":"banana" ,"w": 1, "x": 2 }, 
 
    { "name":"cherry" ,"w": 1, "x": 2 }, 
 
]; 
 
var object2 = [ 
 
    { "type":"banana" ,"y": 3, "x": 4 }, 
 
    { "type":"cherry" ,"y": 3, "x": 4 }, 
 
]; 
 

 
function mergeObject(obj1,obj2){ 
 
    var obj3 = {}; 
 
    for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; } 
 
    for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; } 
 
    return obj3; 
 
} 
 

 
function mergeObjectInArray(firstArray, firstArrayKey, secondaryArray, secondaryArrayKey) { 
 
    var resultArray = new Array(); 
 
    for(var firstArrayIndex in firstArray) { 
 
    var firstArrayObject = firstArray[firstArrayIndex]; 
 
    var resultArrayObject = firstArrayObject; 
 
    for(var secondaryArrayIndex in secondaryArray) { 
 
    var secondaryArrayObject = secondaryArray[secondaryArrayIndex]; 
 
     if(firstArrayObject[firstArrayKey] === secondaryArrayObject[secondaryArrayKey]) { 
 
     resultArrayObject = mergeObject(firstArrayObject,secondaryArrayObject); 
 
     delete resultArrayObject[secondaryArrayKey]; 
 
     } 
 
    } 
 
    resultArray.push(resultArrayObject); 
 
    } 
 
    return resultArray; 
 
    
 
} 
 

 
var resultArray = mergeObjectInArray(object1, "name", object2, "type"); 
 

 

 
// Assuming JSON.stringify - not available in IE<8 
 
console.log(JSON.stringify(resultArray)); 
 

 

相關問題