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,435
  • 8
  • 30
  • 51

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,838
  • 7
  • 23
  • 38
1

You can use the following adjustment:

this.client = Object.assign({}, this.client,  {name: this.source.name})
connexo
  • 53,704
  • 14
  • 91
  • 128
0

I would use the ES6 spread operator syntax for this

const b = {iam b}
const a = {...b}
console.log(a) // {iam b}
connexo
  • 53,704
  • 14
  • 91
  • 128
Paulquappe
  • 144
  • 2
  • 6
  • 15