Igor Popov Igor Popov - 4 months ago 23
Javascript Question

How to duplicate object properties in another object?

Given the object:

var firstObject = {
key1 : 'value1',
key2 : 'value2'
};


how can I copy the properties inside another object (
secondObject
) like this:

var secondObject = {
key1 : 'value1',
key2 : 'value2',
key3 : 'value3',
key4 : 'value4'
};


using a reference to the
firstObject
? Something like this:

var secondObject = {
firstObject,
key3 : 'value3',
key4 : 'value4'
};


(this doesn't work... I put it just to show in big lines how I would like to structure the code).

Is a solution possible without using any JavaScript frameworks?

Answer
for(var k in firstObject) secondObject[k]=firstObject[k];