Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  93] [ 5]  / answers: 1 / hits: 19435  / 7 Years ago, thu, february 9, 2017, 12:00:00

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?


More From » javascript

 Answers
231

You can use Object.assign function



obj = {
cadSocios : true
};
obj2 = {
name : ['name1', 'name2'],
part : ['part1', 'part2']
};

merged = Object.assign(obj, obj2);


working jsfiddle


[#59001] Wednesday, February 8, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaidyn

Total Points: 633
Total Questions: 102
Total Answers: 100

Location: Trinidad and Tobago
Member since Thu, Dec 1, 2022
1 Year ago
;