2013-10-23 111 views
0

我有2個JSON投入使用兩條JSONs:的Javascript:合併基於其他字段

[ 
    { 
    userId: 32159, 
    userFirstName: "john", 
    userLastName: "doe", 
    plans: [ ] 
    }, 
    { 
    userId: 32157, 
    userFirstName: "dave", 
    userLastName: "mess", 
    plans: [ ] 
    } 
] 

[ 
     { 
     userId: 32159, 
     userFirstName: "john", 
     userLastName: "doe", 
     results: [ ] 
     }, 
     { 
     userId: 32157, 
     userFirstName: "dave", 
     userLastName: "mess", 
     results: [ ] 
     } 
] 

,我想輸出爲:

[ 
     { 
     userId: 32159, 
     userFirstName: "john", 
     userLastName: "doe", 
     plans: [ ], 
     results: [ ] 
     }, 
     { 
     userId: 32157, 
     userFirstName: "dave", 
     userLastName: "mess", 
     plans: [ ], 
     results: [ ] 
     } 
] 

如何在javascript.Please幫助。這裏userId是一個獨特的領域

+0

你只是想簡單地添加2套? – IProblemFactory

+0

我們可以假設數組的大小相同嗎?這些人是否被分類? – IProblemFactory

回答

1

我想你想extend每個對象,這可以通過集合的一個循環,然後選擇相應的對象,並擴展它,像這樣實現...

var collectionA = [], collectionB = [], 
    result = []; 

$.each(collectionA, function(i, objA) { 
    $.each(collectionB, function(j, objB) { 
     if (objA.userId === objB.userId) { 
      result.push($.extend({}, objA, objB); 
      return false; 
     } 
    }); 
}); 

console.log(result); 
0

使用jQuery的延伸功能:

var json1 = [{ 
    userId: 32159, 
    userFirstName: "john", 
    userLastName: "doe", 
    plans: [] 
}, { 
    userId: 32157, 
    userFirstName: "dave", 
    userLastName: "mess", 
    plans: [] 
}]; 

var json2 = [{ 
    userId: 32157, 
    userFirstName: "dave", 
    userLastName: "mess", 
    results: [] 
}, { 
    userId: 32159, 
    userFirstName: "john", 
    userLastName: "doe", 
    results: [] 
}]; 

var jsonCombined = []; 

for (i = 0; i < json1.length; i++) { 
    for (i2 = 0; i2 < json2.length; i2++) { 
     if (json1[i].userId == json2[i2].userId) 
      jsonCombined.push($.extend(json1[i], json2[i2])); 
    } 
} 

console.log(jsonCombined); 

小提琴here

0

可以用戶下劃線min.js文件,並嘗試下面通過使用其inbuild功能

_.each(json1, function(val1) { 
    _.each(json2, function(val2) { 
      if (val1.userId = val2.userId) { 
       _.extend(val1, val2.results); 
      } 
     } 

    }) 

    }); 
0

你可以用純JavaScript來做到這一點。一種解決方案可能是這樣的。

function copy(obj1, obj2) { 
    for (var x in obj1) 
     if (obj1.hasOwnProperty(x)) 
      obj2[x] = obj1[x]; 
} 

function merge(arr1, arr2) { 
    var fin = []; 

    for (var i = 0; i < arr1.length; i++) { 
     fin.push({}); 
     copy(arr1[i], fin[i]); 
    } 

    for (var i = 0; i < arr2.length; i++) 
     copy(arr2[i], fin[i]); 

    return fin; 
} 

merge(yourfirstobject, yoursecondobject); 
0

你想做的事情叫做功能編程中的同時處理。 你有兩塊數據,你想一起處理它們以得到結果。考慮以下代碼

http://repl.it/Ll1/1

// Array Array -> Array 
// Assume that length of a is equal to length of b 
// and items sorted in the right way to match records 
function zip(a,b) { 
    // Object Object -> Object 
    function add_results(item_a,item_b) { 
     item_a["results"] = item_b["results"] 
     return item_a; 
    } 
    if(a.length===0) { 
     return []; 
    } else { 
     return [add_results(a[0],b[0])].concat(zip(a.slice(1),b.slice(1))); 
    } 
} 

zip(a,b); 
0

我分配工作到離散函數:

function mergeObjects(a, b) { 
    if (a && b) { for (var key in b) { a[key] = b[key]; } } 
    return a; 
} 

function mergeAndSortArrays(a, b) { 
    return a 
     .concat(b) 
     .sort(function (a, b) { return a.userId - b.userId; }); 
} 

function combine(a, b) { 
    var a = mergeAndSortArrays(a, b); 
    for (var i = 0, l = a.length - 1; i < l; i++) { 
     if (a[i].userId === a[i+1].userId) { 
      a[i] = mergeObjects(a[i], a[i+1]); 
      a.splice(i+1, 1); 
      i--; l--; 
     } 
    } 
    return a; 
} 

combine(a, b); 
相關問題