1

I have following JavaScript objects (new_object and old_object):

new_object:

0300 : ["295", "293", "298"],
0800 : ["293", "298"],
0930 : ["295"],
1130 : ["297", "293", "298"],
1230 : ["295"]

old_object:

0300 : ["297"],
0800 : ["297"],
0930 : ["293"],

I want to merge them so that final object would be like

0300 : ["295", "293", "298", "297"],
0800 : ["293", "298", "297"],
0930 : ["295", "298", "293"],
1130 : ["297", "293", "298"],
1230 : ["295", "298"]

I tried new_object['0300'].push with each loop but its not working

How could I merge JavaScript objects ? Are there any best practices?

3
  • Could you include the code you have tried with? Could you explain what exactly is not working, maybe giving us the output you currently get? Commented Oct 25, 2015 at 7:52
  • I don't understand the logic. Why would the 0930 key in the result contain 298? Also, be careful with your keys: if you mean them as strings, fine, but 0300 is an octal constant which means 192. Commented Oct 25, 2015 at 7:53
  • yes the keys are strings Commented Oct 25, 2015 at 8:50

5 Answers 5

1

try this :

for (var key in new_object){
    if(old_object.hasOwnProperty(key)){
        new_object[key] = new_object[key].concat(old_object[key]);
    } 
}
Sign up to request clarification or add additional context in comments.

10 Comments

Won't this concat undefined if the key is missing in old_object?
@torazaburo if there are some missing key in it , insert an if condition before concat to check the validity of old_object[key]
What if old_object has properties that are not in new_object?
@torazaburo object's key are known as properties of object. by checking this condition you are checking if the key of old_object is valid or not
@torazaburo i updated the answer to consider the case you mentioned that is "if old_object has properties that are not in new_object
|
0

You can also use jquery $.merge()

var result = $.merge(oldArray, newArray);

check this

3 Comments

Did you test this? I don't think it will do what you want. Also, in general, unless the OP tagged his question jQuery a non-jQuery answer is preferred.
yes @torazaburo I have tested this, please check working jsfiddle jsfiddle.net/vinodhan/hootuoph
Your fiddle merges individual property values. The job is to merge each pair of properties. See jsfiddle.net/hootuoph/1.
0

I think new_object['0300'].push is a wrong syntax. Try to new_object[0300].push. It works. But output is something strange.

var newObject = {0300 : ["295", "293", "298"],0800 : ["293", "298"],0930 : ["295"],1130 : ["297", "293", "298"],1230 : ["295"]}
var oldObject = {0300 : ["297"],0800 : ["297"],0930 : ["293"]}
newObject[0300].push(oldObject[0300])
//console outputs: ["295", "293", "298", ["297"]] 

So I use concat method.

var newObject = {0300 : ["295", "293", "298"],0800 : ["293", "298"],0930 : ["295"],1130 : ["297", "293", "298"],1230 : ["295"]}
var oldObject = {0300 : ["297"],0800 : ["297"],0930 : ["293"]}

newObject[0300].concat(oldObject[0300])
//console outputs: ["295", "293", "298", "297"]

Finally, Merge two Json objects. Try this.

And see this Questions > How can I merge properties of two JavaScript objects dynamically?

for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; }

Comments

0

First, write a utility function to merge properties with the same key in two objects, based on a merge strategy passed in as a function.

To merge properties with the same key in two objects, we loop over the second list of keys, and either merge the value into the first object (if it has a property with same key), or simply copy it over (if it doesn't). The way this is written it will mutate (overwrite) o1:

function merge_keys(o1, o2, merge) {
  var keys2 = Object.keys(o2), key;

  for (var i = 0; i < keys2.length; i++) {
    key = keys2[i];
    o1[key] = o1[key] ? merge(o1[key], o2[key]) : o2[key];
  }
}

Define the merging strategy you want to use:

function concat(a, b) { return a.concat(b); }

Then

merge_keys(new_object, old_object, concat);

See http://jsfiddle.net/hootuoph/2/.

If you are programming in ES6, you can write this more compactly using for...of:

function merge_keys(o1, o2, merge) {
  for (var k of Object.keys(o2)) {
    o1[k] = o1[k] ? merge(o1[k], o2[k]) : o2[k];
  }
}

Or if you prefer using Array#forEach:

function merge_keys(o1, o2, merge) {
  function maybe_merge(k) { o1[k] = o1[k] ? merge(o1[k], o2[k]) : o2[k]; }

  Object.keys(o2).forEach(maybe_merge);
}

Comments

0

Actually made a proper solution...

var final_object = {};

var new_object_properties = Object.getOwnPropertyNames(new_object);
var old_object_properties = Object.getOwnPropertyNames(old_object);
var new_object_properties_length = new_object_properties.length;
var old_object_properties_length = old_object_properties.length;
if(new_object_properties_length >  old_object_properties_length)
while(--new_object_properties_length){
     var property_name = new_object_properties[new_object_properties_length];
     if(old_object[property_name])
          final_object[property_name] = new_object[property_name].concat(old_object[property_name];
     else
         final_object[property_name] = new_object_properties[new_object_properties_length];
}
else
    while(--old_object_properties_length){
     var property_name = old_object_properties[old_object_properties_length];
     if(new_object[property_name])
          final_object[property_name] = old_object[property_name].concat(new_object[property_name];
     else
         final_object[property_name] = old_object_properties[old_object_properties_length];
}

2 Comments

This algorithm has the rather unfortunate characteristic that for keys in both old and new objects, it will concatenate once in the first loop, then throw that result away and concatenate again in the second loop.
Yeah, I am confident that there is a better way, just the one I came up with quickly. EDIT: Oh i see its an easy fix :D

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.