Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between angular.copy() and JSON.parse(JSON.stringify())?

Can someone explain the differences between angular.copy() and JSON.parse(JSON.stringify())? Are there any? What you will recommend to use? Is angular.fromJson(angular.toJson()) the same as JSON.parse(JSON.stringify())?

Just to mention, I've read How do I correctly clone a JavaScript object? for JSON.parse(JSON.stringify()) and angular.copy() reference for angular.copy().

like image 542
Marin Takanov Avatar asked Apr 21 '15 18:04

Marin Takanov


People also ask

What is the difference between JSON Stringify and JSON parse?

parse() is used for parsing data that was received as JSON; it deserializes a JSON string into a JavaScript object. JSON. stringify() on the other hand is used to create a JSON string out of an object or array; it serializes a JavaScript object into a JSON string.

Does JSON parse JSON Stringify deep copy?

If your deeply-nested object or array only includes certain primitive values (strings, numbers, boolean, and null), then you can use JSON. parse() & JSON. stringify() to deep copy without issues.

What is the use of JSON parse and JSON Stringify?

The JSON. parse() function is used to convert a string into a JavaScript object while the JSON. stringify() function is used to convert a JavaScript object into a string.

What is JSON Stringify in angular?

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.


1 Answers

What JSON.parse(JSON.stringify()) won't copy:

  • functions
  • any object that has a special representation, like Date (it will get copied but not as Date)
  • properties with the value undefined

angular.fromJson(angular.toJson()) is basically the same except that angular.toJson() omits properties that are used by Angular internally (those starting with $$).

like image 193
a better oliver Avatar answered Oct 21 '22 13:10

a better oliver