Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and clone a JSON object?

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

like image 404
mateusmaso Avatar asked Nov 08 '10 00:11

mateusmaso


People also ask

How do I copy a JSON object?

By using both, we first make a JSON string from the object we want to copy ( JSON. stringify(objectToCopy) ). Then, we take that JSON string and make a new object from it (with JSON. parse() ).


1 Answers

This is what I do and it works like a charm

if (typeof JSON.clone !== "function") {     JSON.clone = function(obj) {         return JSON.parse(JSON.stringify(obj));     }; } 
like image 160
Matías Cánepa Avatar answered Sep 23 '22 18:09

Matías Cánepa