Vikram
Vikram

Reputation: 3351

Merging array-valued properties in two objects

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?

Upvotes: 1

Views: 92

Answers (5)

Olavi Sau
Olavi Sau

Reputation: 1649

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];
}

Upvotes: 0

MohammadJavad Seyyedi
MohammadJavad Seyyedi

Reputation: 684

try this :

for (var key in new_object){
    if(old_object.hasOwnProperty(key)){
        new_object[key] = new_object[key].concat(old_object[key]);
    } 
}

Upvotes: 1

user663031
user663031

Reputation:

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

Upvotes: 0

Minwoo
Minwoo

Reputation: 243

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]; }

Upvotes: 0

Vinodhan
Vinodhan

Reputation: 110

You can also use jquery $.merge()

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

check this

Upvotes: 0

Related Questions