0

我有 2 个这样的 json 输入:

[
  {
    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 中做到这一点。请帮助。这里 userId 是一个唯一字段

4

6 回答 6

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);
于 2013-10-23T11:39:26.357 回答
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);

在这里提琴

于 2013-10-23T11:40:11.490 回答
0

您可以使用 underscore-min.js 文件并使用其 inbuild 功能尝试如下

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

    })

  });
于 2013-10-23T11:41:46.450 回答
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);
于 2013-10-23T11:42:05.047 回答
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);
于 2013-10-23T12:06:26.197 回答
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);
于 2013-10-23T12:32:50.330 回答