let me={
name:"Shivendra",
age:21
}
let you={
name:"Neha",
age:22
}
let mergeMeAndYou={
...me, ...you
}
console.log(mergeMeAndYou);
I am getting the output:- { name: 'Neha', age: 22 }
Now I was not expecting this. Can any one explain this result ? & how I will now get the merged object ? I am using node version 8.9.4.
You can do this by enclosing the object with {
and }
, like: {...obj}
let me={
name:"Shivendra",
age:21
}
let you={
name:"Neha",
age:22
}
//Putting the me and you on an array.
let mergeMeAndYou = [
{...me},{ ...you}
]
console.log(mergeMeAndYou);
//Putting the me and you on an object.
let mergeMeAndYou2 = {
me:{...me}, you:{ ...you}
}
console.log(mergeMeAndYou2);
Note: Based on how you want to combine the objects, you don't really need to Spread the objects. You can just:
let mergeMeAndYou = [me,you];
let mergeMeAndYou2 = {me:me, you:you}