0

I'm trying to copy the name from one object to another as below:

console.log(this.source.name)

//gives "mike"

mounted: function() {
   this.client.name = Object.assign({}, this.source.name)
}

then

console.log(this.client.name)

//gives object with 1: m, 2: i, 3: k, 4:e

What I'm doing wrong? How should I correct my code?

gileneusz
  • 1,299
  • 8
  • 24
  • 49

3 Answers3

1

Object.assign takes two object params, but you're passing a string to second param. So, if you want to assign string value simply do this.client.name = this.source.name.

If you want to copy object value, use Object.assign and store an object in this.source.name as this.source.name={"key":"value"};.

Amanshu Kataria
  • 2,180
  • 5
  • 20
  • 37
1

You can use the following adjustment:

this.client = Object.assign({}, this.client,  {name: this.source.name})
connexo
  • 49,059
  • 13
  • 74
  • 108
0

I would use the ES6 spread operator syntax for this

const b = {iam b}
const a = {...b}
console.log(a) // {iam b}
connexo
  • 49,059
  • 13
  • 74
  • 108
Paulquappe
  • 155
  • 1
  • 7
  • 15