简体   繁体   中英

How to deep copy an object in javascript as object not as array

How to deep copy an object in javascript as object not as array

var x = {attr1: "value", attr2: "value"};
var y = $.extend(true, [], x);
var z = $.extend(true, [], x);

alert($.type(x)); // object
alert($.type(y)); // array [ why not object too ]
alert($.type(z)); // array [ why not object too ]

Instead of

var y = $.extend(true, [], x);

Try

var y = $.extend(true, {}, x);
                       ^^--------- Empty object instead of empty Array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM