如何在jQuery中合并具有相同键名的两个对象?

时间:2019-04-12 07:33:28

标签: jquery object

我有两个对象

Object1

0: {count: "100", keyFrom: "2"}
1: {count: "200", keyFrom: "4"}
2: {count: "300", keyFrom: "8"}

Object2

0: {count: "400", keyFrom: "3"}
1: {count: "500", keyFrom: "9"}
2: {count: "600", keyFrom: "7"}
3: {count: "700", keyFrom: "1"}
4: {count: "900", keyFrom: "5"}

我想合并这两个对象。我尝试使用

var objects = {};
$.extend(objects, object1, object2);

但这给我的结果是,

0: {count: "100", keyFrom: "2"}
1: {count: "200", keyFrom: "4"}
2: {count: "300", keyFrom: "8"}
3: {count: "700", keyFrom: "1"}
4: {count: "900", keyFrom: "5"}

它忽略了object2中的前三个值。 但我想合并,

0: {count: "100", keyFrom: "2"}
1: {count: "200", keyFrom: "4"}
2: {count: "300", keyFrom: "8"}
3: {count: "400", keyFrom: "3"}
4: {count: "500", keyFrom: "9"}
5: {count: "600", keyFrom: "7"}
6: {count: "700", keyFrom: "1"}
7: {count: "900", keyFrom: "5"}

帮我解决问题。

3 个答案:

答案 0 :(得分:2)

如果它们是数组,则只需使用concat

var new_object = object1.concat(object2);

实时示例:

var object1 = [
    {count: "100", keyFrom: "2"},
    {count: "200", keyFrom: "4"},
    {count: "300", keyFrom: "8"}
];
var object2 = [
    {count: "400", keyFrom: "3"},
    {count: "500", keyFrom: "9"},
    {count: "600", keyFrom: "7"},
    {count: "700", keyFrom: "1"},
    {count: "900", keyFrom: "5"}
];

var new_object = object1.concat(object2);
console.log(new_object);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

如果它们是具有这些属性名称的非数组对象,并且您想要数组作为结果,则可以使用新的Object.values(可以填充)和扩展符号(ES2015 +)或{{1 }}(ES5):

Array.prototype.push.apply

var new_object = Object.values(object1);
new_object.push(...Object.values(object2));

实时示例:

var new_object = Object.values(object1);
Array.prototype.push.apply(new_object, Object.values(object2));

如果它们是具有这些属性名称的非数组对象,并且您想要具有相似属性名称的非数组对象,则可以使用Object.assignvar object1 = { 0: {count: "100", keyFrom: "2"}, 1: {count: "200", keyFrom: "4"}, 2: {count: "300", keyFrom: "8"} }; var object2 = { 0: {count: "400", keyFrom: "3"}, 1: {count: "500", keyFrom: "9"}, 2: {count: "600", keyFrom: "7"}, 3: {count: "700", keyFrom: "1"}, 4: {count: "900", keyFrom: "5"} }; var new_object = Object.values(object1); new_object.push(...Object.values(object2)); // Or in ES5 with an `Object.values` polyfill // Array.prototype.push.apply(new_object, Object.values(object2)); console.log(new_object);的内置版本在ES2015 +中)或之后的$.extend

$.extend

var new_object = Object.values(object1);
new_object.push(...Object.values(object2));
new_object = Object.assign({}, new_object);
var object1 = {
    0: {count: "100", keyFrom: "2"},
    1: {count: "200", keyFrom: "4"},
    2: {count: "300", keyFrom: "8"}
};
var object2 = {
    0: {count: "400", keyFrom: "3"},
    1: {count: "500", keyFrom: "9"},
    2: {count: "600", keyFrom: "7"},
    3: {count: "700", keyFrom: "1"},
    4: {count: "900", keyFrom: "5"}
};

var new_object = Object.values(object1);
new_object.push(...Object.values(object2));
new_object = Object.assign({}, new_object);
console.log(new_object);

答案 1 :(得分:0)

您需要将它们转换为数组并合并:

var array1 = $.map(object1, function(value, index) {
    return [value];
});
var array2 = $.map(object2, function(value, index) {
    return [value];
});
var result = $.merge($.merge([], array1, array2);

或更简单,结合上一个答案:

var result = $.merge($.merge([], Object.values(object1)), Object.values(object2));

答案 2 :(得分:0)

请仅在数组中使用它,然后您才能根据需要进行连接。请参考以下代码

let x = [{count: "100", keyFrom: "2"} ,{count: "200", keyFrom: "4"},{count: "300", keyFrom: "8"}];
let y = [{count: "400", keyFrom: "3"},{count: "500", keyFrom: "9"},{count: "600", keyFrom: "7"},
         {count: "700", keyFrom: "1"},{count: "900", keyFrom: "5"}];
console.log(x.concat(y));