使用push-on-item值合并两个json数组

merge two json arrays with push on item value

本文关键字:两个 json 数组 push-on-item 合并 使用      更新时间:2023-09-26

我有以下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

知道吗?

试试这个逻辑:

 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);

您可以使用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

以下是处理数据的通用方法:

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);