Skip to content Skip to sidebar Skip to footer

Problem In Concatenation Of Objects In Javascript

I have problem in concatenating objects in java script Eg: var firstObj = {}; firstObj.info = ['sam','kam']; var secObj = {}; secObj.info = ['ram','dam']; Output that i need

Solution 1:

firstObj.info = firstObj.info.concat(secObj.info);

The only way to do this is to simply overwrite the info property of object with the conocated array stored at info property of this object and the other obj (or more objects as concat takes multiple number of arguments)

Solution 2:

Using concat:

var firstObj = {};
firstObj.info = ["sam","kam"];  
var secObj = {};
secObj.info = ["ram","dam"];

var result = firstObj.info;

result = result.concat(secObj.info);

// result = {"sam","kam","ram","dam"}

http://jsbin.com/ahaxu3

Solution 3:

If you have two objects of the same structure you should write a concatenation function which concatenates every single variable of that kind of object. You need to take all possible cases in account.

For regular variable types as Strings or simple Arrays this seems easy. You may use the concat function for Arrays and + to concatenate Strings, but it will get difficult if you want to concatenate variables holding complexe objects.

Post a Comment for "Problem In Concatenation Of Objects In Javascript"