2014-09-26 45 views
0

我有以下的JSON數組:合併與推2個JSON數組的項目值

陣列1:

fruits1 = [{"fruit":"banana","amount":"2","color":"yellow"},{"fruit":"apple","amount":"5","color":"red"},{"fruit":"kiwi","amount":"1","color":"green"}] 

陣列2:

fruits2 = [{"fruit":"banana","sold":"1","stock":"3"},{"fruit":"apple","sold":"3","stock":"5"},{"fruit":"kiwi","sold":"2","stock":"3"}] 

我希望得到的只是一個根據水果值合併結果的陣列如下:

fruits = [{"fruit":"banana","amount":"2","color":"yellow","sold":"1","stock":"3"},{"fruit":"apple","amount":"5","color":"red","sold":"3","stock":"5"},{"fruit":"kiwi","amount":"1","color":"green","sold":"2","stock":"3"}] 

我需要做的是這樣

foreach item.fruit where fruit = fruit from initial array 
    fruits.push item 

任何想法?

+1

[也許這個鏈接可能是有用的](http://stackoverflow.com/questions/14164060/combine-merge-objects-in-array-based-upon-common-value) – Razorphyn 2014-09-26 16:33:30

+0

謝謝。然而,我不打算組合。讓我更新問題 – domi771 2014-09-26 16:35:01

+0

我在考慮[$ .extend](http://api.jquery.com/jquery.extend/),但如果我沒有錯,它會根據對象鍵的名稱進行合併,在這種情況下,您應該將該值與該值交換併合並,然後再次交換,但我不認爲它是非常優化的。另一個解決方案是一個間接的互動,但仍然不是很好,讓我們等待一個比我更有能力的人 – Razorphyn 2014-09-26 16:46:09

回答

1

嘗試這樣的邏輯:

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


var obj1 = []; 
for (var i = 0; i < fruits1.length ; i++) { 
    obj1[fruits1[i].fruit] = fruits1[i]; 
} 

var obj2 = []; 
for (var i = 0; i < fruits2.length ; i++) { 
    obj2[fruits2[i].fruit] = fruits2[i]; 
} 

var fruits = [] 
for (var key in obj1) { 
    fruits.push(merge_options(obj1[key],obj2[key])); 
} 

    console.log(fruits); 
+0

最終這是對我來說工作/更好的解決方案。 – domi771 2014-09-29 16:33:15

0

你可以做這樣的事情與JavaScript

// create a hash like {fruit_name -> object} 
f1 = {}; 
fruits1.forEach(function(p) { 
    f1[p.fruit] = p; 
}); 

// merge second array into above hash on fruit_name 
fruits2.forEach(function(p) { 
    for (var a in p) { f1[p.fruit][a] = p[a];} 
}); 

//fruits1 will now contain result; 
//if you don't want to spoil fruit1 array, clone p inside 'fruits1.forEach' above before assigning it to 'f1[p.fruit]'. And at the end, create a new array out of f1 
+0

確定所有工作 - 謝謝 – domi771 2014-09-26 17:43:08

0

這裏是與您的數據工作的通用方式:

function joinObjects(initial, other, predicate, valueSelector) { 
    if(typeof(predicate) !== 'function') throw 'predicate must be a function'; 
    if(typeof(valueSelector) !== 'function') throw 'valueSelector must be a function'; 

    // make a clone of the original object so its not modified 
    var clone = jQuery.extend(true, {}, initial); 

    // iterate over the initial and other collections 
    for(var cloneKey in clone) { 
     if (!clone.hasOwnProperty(cloneKey)) continue; 
     for(var otherKey in other) { 
      if (!other.hasOwnProperty(otherKey)) continue; 

      // if the predicate is truthy, get the values 
      if (predicate(clone[cloneKey], other[otherKey])) { 

       // pull only the values you want to merge 
       var values = valueSelector(other[otherKey]); 

       // iterate over the values add them to the cloned initial object 
       for(var valueKey in values) { 
        if (values.hasOwnProperty(valueKey)) { 
         clone[cloneKey][valueKey] = values[valueKey]; 
        } 
       } 

      } 
     } 
    } 

    return clone; 
} 

var fruits1 = [{"fruit":"banana","amount":"2","color":"yellow"},{"fruit":"apple","amount":"5","color":"red"},{"fruit":"kiwi","amount":"1","color":"green"}]; 
var fruits2 = [{"fruit":"banana","sold":"1","stock":"3"},{"fruit":"apple","sold":"3","stock":"5"},{"fruit":"kiwi","sold":"2","stock":"3"}]; 
var finalFruits = joinObjects(fruits1, fruits2, 
    function(left, right) { return left.fruit == right.fruit }, 
    function(other) { 
     return { 
      sold: other.sold, 
      stock: other.stock 
     }; 
    }); 

console.log(finalFruits);