0

I'm using redux, then I need to be able to remove a key from object without mutating it.

But when removing a netsed key from a clone, the original object is also mutated. Like this :

let t1 = {a: { a1: 'test' }}
let t2 = Object.assign({}, t1}
delete t2['a']['a1']
t2 // { a: {} } // ok.
t1 // { a: {} } // Not ok !

Any hint ?

Atopus
  • 35
  • 7

1 Answers1

0

All right. I missed the Shallow/deep copy distinction.

So for now, I end up with :

let t1 = { a: { a1: 'test'}}
let t2 = JSON.parse(JSON.stringify(t1))
delete t2['a']['a1']
t2 // { a: {} }
t1 // { a: { a1: 'test' } }
Atopus
  • 35
  • 7