score:5

Accepted answer

For deep copy try this

  let _result = JSON.parse(JSON.stringify(steps));

or

  const newArray = steps.map(step => ({...step}));

spread syntax doesn't give a deep copy

Deep copy

A deep copy means actually creating a new array and copying over the values, since whatever happens to it will never affect the origin one.

For more clarification and the better understanding, you can refer How do you clone an Array in Javascript?

score:0

Parsing and Un-Parsing can be quite a performance hit depending on the situation, One convenient alternate way would be that if you use 'Lodash' in your project you can simply use cloneDeep like so:

const newArr = _.cloneDeep( steps );

This returns a new array, leaving the original one untouched.


Related Query

More Query from same tag