0

I have two objects

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"}

I want to merge these two objects. I tried using

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

But this gives me a result like,

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"}

It omits the first three values in object2. But I want to merge like,

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"}

Help me to solve the problem.

4
  • Are they arrays, or non-array objects? Commented Apr 12, 2019 at 7:37
  • It looks like these are two arrays containing objects. So you can just use concat? Commented Apr 12, 2019 at 7:38
  • three key are not updating bz json object cannot have duplicte keys,so key 0,1,2 is already present in the first object so it will take again from the second object. Commented Apr 12, 2019 at 8:11
  • check out my answer bro it might help you Commented Apr 12, 2019 at 10:11

3 Answers 3

2

If they're arrays, just use concat:

var new_object = object1.concat(object2);

Live Example:

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>

If they're non-array objects with those property names and you want an array as a result, you can use the newish Object.values (which can be polyfilled) and spread notation (ES2015+) or Array.prototype.push.apply (ES5):

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

or

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

Live Example:

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));
// Or in ES5 with an `Object.values` polyfill
// Array.prototype.push.apply(new_object, Object.values(object2));
console.log(new_object);

If they're non-array objects with those property names and you want a non-array object with similar property names, you can use Object.assign (which is the built-in version of $.extend in ES2015+) or $.extend afterward:

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);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

Sir, var new_object = Array.prototype.concat.call(data.experienceList.experienceTo, data.experienceList.experienceFrom); is working for me
@selva - Interesting. I removed that from my answer because what it created didn't look like what you wanted. But if it does, great!
0

You need to convert them to arrays and merge:

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

Or even simpler, combining with the previous answer:

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

Comments

0

Please use it in a array only then you can concat as u need.please refer the below code

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.