How to keep a copy of an old list or clone a list in Javascript , also how to transform js object to json string also json string to js object

would show you how to clone a list or keep a copy of an old list (array , object , etc ) , would also explain how to convert a javascript object into a json string and convert a json string into a javascript object
Using json.stringify() alone good but it can’t save unsupported data ,
json.stringify() takes in a javascript object and turns it into a json string or we can say it’s a way to change a list(array , object , etc) into a string that could be easily printed to the screen ,
var keepacopy = json.stringify(arry);
this is an example above , it could keep a copy but some stuffs might not be supported, The solution is to use is
json.parse(json.stringift ()) ,
var keepacopy =
all thought it takes little time but it helps converted unsupported data into supported data .
json.parse takes a json string and change into a java script object.

please help like article on medium and follow me @abodmicheal

If the object isn’t able to serialize to JSON, chances are it’s not serializable at all. A network socket for example. You can’t copy those.

You can shallow clone an obj with newobj = {...oldobj}. Just change the brackets for an array. There are deep clone libraries out there you can use, but roundtripping the object through JSON is still a perfectly good technique you’ll see everywhere.

The real solution is to use immutable data in the first place. You can copy or not copy immutable data as you please, and it’s all the same to whatever uses it. For that I’d suggest using something like Immer which gives you a principled way to express changes rather than just deep-freezing your objects and making you fend for yourself.

1 Like