Cloning a json object and changing values mutates the original object as well

前端 未结 5 1179
天涯浪人
天涯浪人 2021-01-01 07:01

I was wondering why does this happen?

I have an json object stored in var myObj:

var myObj = JSON.parse(fs.readFileSync(\'json/data.json\', \'utf8\')         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-01 07:34

    If you use jQuery, then you can do this

    var mobObj = jQuery.extend(true, {}, myObj);
    

    else try this

    var mobObj = Object.assign({}, myObj);
    

提交回复
热议问题