m24lsq
Last Updated: June 09, 2018
·
1.873K
· fr0gs
Doggy

Deep Copy object in Javascript

Full credit to [http://geniuscarrier.com/copy-object-in-javascript/][this webpage] who gathered several examples. This is just a nice reminder for me to have everything in the same place.

function deepCopy(oldObj) {
    var newObj = oldObj;
    if (oldObj && typeof oldObj === 'object') {
        newObj = Object.prototype.toString.call(oldObj) === "[object Array]" ? [] : {};
        for (var i in oldObj) {
            newObj[i] = deepCopy(oldObj[i]);
        }
    }
    return newObj;
}
Say Thanks
Respond

3 Responses
Add your response

28113
D487ed147b53832e540efd50b110bf64

as far as i know JSON.parse(JSON.stringify(obj))1 is faster sadly the page with the benchmarks is currently under development

over 1 year ago ·
28114
Doggy

Nice trick! as soon as it is up I'll test it :D thank you

over 1 year ago ·
29995

Objects in javascript : object.assign/deep copy. The Object constructor creates an object wrapper for the given value. If the value is null or undefined, it will create and return an empty object, otherwise, it will return an object of a Type that corresponds to the given value.
https://www.techavy.com/best-fps-gaming-mouse/ l https://freemoviesite.me/ l https://frpbypassapk.net/ l https://kodipair.com/iptv-kodi-best-addons/

2 months ago ·