salmanaacvf
salmanaacvf

Reputation: 257

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}]
console.log(var2)

Upvotes: 1

Views: 78

Answers (5)

Yogendra
Yogendra

Reputation: 1300

the (...) is Array/Object spread operator js spread operator

What the code above does, is to spread over the d object and get all its properties, then overwrite the existing properties with the ones we’re passing. It copies the properties of the d object, over to the newly created object. So you are getting this output. Try the below code:

<script>
const d = [{name : '',age : '',day :23}]
const var2 =  [{...d[0], name : 'david', age : 22}]
console.log(var2)

</script>

You can also achieve the same using below code

<script>
const d = {name : '',age : '',day :23}
const var2 =  {...d, name : 'david', age : 22}
console.log(var2)
</script>

It will override the name and age

Upvotes: 0

thchp
thchp

Reputation: 2374

You are not using the spread operator correctly. You should use it to merge all elements of d[0] with an object containing your updated values.

const d = [{name : '',age : '',day :23}]

// for d containing only one element 
const var2 =  [{...d[0], name : 'david', age : 22}]
// will result in [{name: '', age: '', day: 23, name: 'david', age: 22}] and be merged by keeping the right-most value

// for all elements in d
const var3 = d.map(el => { 
  return {...el, name : 'david', age : 22}
})

console.log(var2)
console.log(var3)

Upvotes: 2

Sparrow
Sparrow

Reputation: 390

To get your expected result {name : "david", age : 23, day : 23} merge two objects like shown below.

merge Object like {...object1, ...object2}

const d = [{name : '',age : '',day :23}]
const var2 =  {...d[0], ...{ name : 'david', age : 22}}
console.log(var2)

Upvotes: 1

ShyEnough
ShyEnough

Reputation: 21

Spread operator you use in line 2 is used when all elements from an object or array need to be included in a list of some kind.

Basically what happens in your code ->

Array d has 1 item, so when you try to define var2 it takes all elements from array d ( 1 in our case ) and add { name : 'david', age : 22} to it.

In result you get array of two items.

For example:

const array1 = [1, 2, 3]
const array2 = [...array1,4, 5]

// array2 now is [1, 2, 3, 4, 5]
console.log(array2)

Upvotes: 0

Dimitar
Dimitar

Reputation: 1180

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}.

Upvotes: 1

Related Questions