Skip to content

why am I having this output as result?

what would be the result of my code ? I am expecting to have as result one single array of length 1 , I want to output only values of d variable that haven’t been updated in my var2 variable but I get an array of length 2 , I want to have output

{name : “david”, age : 23, day : 23}

const d = [{name : '',age : '',day :23}]
const var2 =  [...d, { name : 'david', age : 22}]


Think of it this way:

d is an array of objects

{ name : 'david', age : 22} is an object literal

When you use the spread operator on d, you are telling it to take each object in d and place it into var2, then at the end, append the { name : 'david', age : 22}.

So the result would be an array with all the items you had in d + the object literal { name : 'david', age : 22}.