Adrian Tache
May 9 · 1 min read

Instead of using your suggested solution to deep copy, which has terrible performance in my experience, my suggestion is to simply create a new object in the loop and manually copy over the values.

Say you have a let obj = {id: 0, name: "zero"} multiple times inside an array, you would create a deep copy of that array this way:

let array = [obj1, obj2]
let newArray = []
array.forEach( element => {
const obj = {
id: element.id,
name: element.name
}

newArray.push(obj)
}

Of course you could skip creating a named object in the loop and just push the new object directly. Furthermore, you could also prevent mutating the new array by using newArray = [...newArray, obj] .

    Adrian Tache

    Written by

    Mobile Developer 📱

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade