1

var a = [{a: '1'}, {a: '2'}, {a: '3'}];

var b = [...a];

b[0].a = 2;

console.log(a)

// result expect is b = [{a: '1'}, {a: '2'}, {a: '3'}];

// but real result is b = [{a: '2'}, {a: '2'}, {a: '3'}];

vân tang
  • 69
  • 5

1 Answers1

1

You have to recreate all items in the new Array:

var b = a.map(i => { return { ...i } })
np42
  • 847
  • 7
  • 13