score:6

Accepted answer

every js object is passed as a reference (objects, arrays, functions etc.). in order to make a 'deep copy' of a particular object you can do:

var deepcopy = json.parse(json.stringify(oldobject)) // 1. - more of a hack
var deepcopy = _.clonedeep(oldobject); // 2. use some predefined methods from jquery, lodash or any other library

var shallowcopy = object.assign({}, oldobject) // 1. - preferred (if you support new es features)

this way your data on the list won't be modified.

score:10

if you only need a shallow copy you can do it like this:

arr.push({...o})

assuming o is the object that you want to push and clone at the same time.


Related Query

More Query from same tag