I'm working with Angular 2 ( typescript)
I have an Object:
obj = {
"cadSocios" : true
};
And I need to add to it other values:
obj2 = {
"name" : ['name1', 'name2'],
part : ['part1', 'part2']
};
My Final Object must be:
objFinal = {
"cadSocios" : true,
"name" : ['name1', 'name2'],
part : ['part1', 'part2']
};
How can I do this? in array can use .push, and Object?
You can use Object.assign
function
obj = {
"cadSocios" : true
};
obj2 = {
"name" : ['name1', 'name2'],
part : ['part1', 'part2']
};
merged = Object.assign(obj, obj2);
working jsfiddle
Object.assign
function, provided by ES6.
var obj1 = {
"cadSocios" : true
};
var obj2 = {
"name" : ['name1', 'name2'],
part : ['part1', 'part2']
};
var obj = Object.assign(obj1, obj2);
console.log(obj);
Simple for in
loop.
var obj1 = {
"cadSocios": true
};
var obj2 = {
"name": ['name1', 'name2'],
part: ['part1', 'part2']
};
for (prop in obj2) {
if (obj2.hasOwnProperty(prop)) {
obj1[prop] = obj2[prop];
}
}
console.log(obj1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With