How to create and clone a JSON object?

后端 未结 8 759
独厮守ぢ
独厮守ぢ 2020-12-13 18:04

I was wondering how can I create a JSON (JS) object and then clone it.

8条回答
  •  被撕碎了的回忆
    2020-12-13 18:35

    As of ES6. Object.assign is a good way to do this.

    newjsonobj = Object.assign({}, jsonobj, {})
    

    The items in the first argument mutate the existing object, and the third argument are changes in the new object returned.

    In ES7 it is proposed that the spread operator be used.

    newjsonobj = {...jsonobj}
    

提交回复
热议问题