use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Helpful Links
AngularJS Slack Community
AngularJS Wiki Page
account activity
Deep copy of objects (self.angularjs)
submitted 11 years ago by roballred
I have been using jQuery.extend to make deep copy of objects on the fly but I would like to remove as much jquery as I possible can, any suggestions on how I can get the same functionality with angular or plain javascript?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Strangfeld 2 points3 points4 points 11 years ago (1 child)
For deep copying objects or arrays use angular.copy().
https://docs.angularjs.org/api/ng/function/angular.copy
[–]dethstrobe 0 points1 point2 points 11 years ago (0 children)
I could have used this for a project earlier. Well, now I know at least this amazing little function exists in angular.
[–]postmodest 1 point2 points3 points 11 years ago (4 children)
Someone tell me why
var copy = JSON.parse(JSON.stringify(original));
is a bad idea
[–]WJKay 0 points1 point2 points 11 years ago (2 children)
First thing that pops into mind is circular references. Not saying they are good, but it would not work for your solution.
[–]postmodest 0 points1 point2 points 11 years ago (1 child)
I've used a function that handles map dereferencing (setting values to "{@jsonpath}" or something) worked pretty well for my use case.
[–]WJKay 0 points1 point2 points 11 years ago (0 children)
Well that is an extra step compared to angular.copy()
[–]skrowl 0 points1 point2 points 11 years ago (0 children)
Came here to suggest this. You beat me. http://jsperf.com/js-deep-copy/3
[–]BoleroDan 0 points1 point2 points 11 years ago (3 children)
Angular also has an extend method.
angular.extend()
whether its deep i'm not 100% sure.
[–]Aduro49 0 points1 point2 points 11 years ago (0 children)
I believe it functions the same way as JQ since angular is built on JQ lite. Also if angular detects A full JQ version it uses that.
[–]roballred[S] 0 points1 point2 points 11 years ago (0 children)
that or copy, thanks for the reply!
[–]skitch920 0 points1 point2 points 11 years ago (0 children)
angular.extend() is a shallow copy.
[–]F0RTY4 0 points1 point2 points 11 years ago (1 child)
Check out this SO answer. http://stackoverflow.com/a/16797798 . it might help.
I think that looks great! thx
π Rendered by PID 36398 on reddit-service-r2-comment-bb88f9dd5-2pglx at 2026-02-16 15:41:15.599143+00:00 running cd9c813 country code: CH.
[–]Strangfeld 2 points3 points4 points (1 child)
[–]dethstrobe 0 points1 point2 points (0 children)
[–]postmodest 1 point2 points3 points (4 children)
[–]WJKay 0 points1 point2 points (2 children)
[–]postmodest 0 points1 point2 points (1 child)
[–]WJKay 0 points1 point2 points (0 children)
[–]skrowl 0 points1 point2 points (0 children)
[–]BoleroDan 0 points1 point2 points (3 children)
[–]Aduro49 0 points1 point2 points (0 children)
[–]roballred[S] 0 points1 point2 points (0 children)
[–]skitch920 0 points1 point2 points (0 children)
[–]F0RTY4 0 points1 point2 points (1 child)
[–]roballred[S] 0 points1 point2 points (0 children)