Shallow copy arrays using slice and the spread operator. Deep copy arrays using JSON.stringify.
đ Code: http://codepen.io/beaucarnes/pen/OWePwd?editors=0012
đŠ Beau Carnes on Twitter: https://twitter.com/carnesbeau
âJavaScript Playlistsâ
â¶JavaScript Basics: https://www.youtube.com/playlist?list=PLWKjhJtqVAbk2qRZtWSzCIN38JC_NdhW5
â¶Data Structures and Algorithms: https://www.youtube.com/playlist?list=PLWKjhJtqVAbkso-IbgiiP48n-O-JQA9PJ
â¶Design Patterns: https://www.youtube.com/playlist?list=PLWKjhJtqVAbnZtkAI3BqcYxKnfWn_C704
â¶ES6: https://www.youtube.com/playlist?list=PLWKjhJtqVAbljtmmeS0c-CEl2LdE-eR_F
â¶Clean Code: https://www.youtube.com/playlist?list=PLWKjhJtqVAbkK24EaPurzMq0-kw5U9pJh
–
We’re busy people who learn to code, then practice by building projects for nonprofits. Learn Full-stack JavaScript, build a portfolio, and get great references with our open source community.
Join our community at https://freecodecamp.com
Read great tech articles at https://medium.freecodecamp.com
source
I don't know why I found it funny that the solution was simply to turn it into JSON and back into Javascript. The creativity of some people đ
Youâre awesome. Thank you so much. I was having such an issue with a nested array. I couldnât understand why it keep muting the original array. Thank you again and keep up the great work.
That will not always work so as you want, if you have a property that points to a function or your working with Date object then you can't use JSON.Parse…….
Better use the spread operator.
his teaching is super confusing …
how to copy this one
var arr=[
1, 3, [5, 6, [7, [6, 5], 4], 3], [4]
];
here deep copy or shallow copy will not work i think?
It feels like very bad language design. Why don't all javascript objects have a copy method for this
Thank you so much!
When copying array of json data into new array using speard operator it's muting original data. Speard operator fails over here. I used to resolve this issue by using lodash đȘđȘ
deep copy can simply done like
const copyArray = […originalArray];
Brilliant
That was really helpful, was stuck with this
Thanx â€ïž
Thank you for the content.
you just saved my life i was keep facing the issue for last couple of days…
Beau this is the best explanation of shallow and deep copy I have seen, thanks alot!!
Very helpful video with detailed explanation
damn… finally found where is my bug thanks to this video… the way this thing work is completely absurd !
thank you
5:47 you're a poet and you didn't know it.
Huge thank you for that JSON tips
Unfortunately, it removes any functional property of the object.
Example:
const names = [
{ value: "name",
getName : function(){ return this.value }}
];
const names2 = JSON.parse(JSON.stringify(names));
names2[0].value = "modified";
names[0].getName()
names2[0].getName(); // this line will throw the error getName() isn't a function.
Dude… I'm already loving you so much. I spent an hour racking my brains with this at work and now it's finally settled.
THANK YOU
Is using JSON better in performance than manually copying each item for a deep copy?
I used this array to test:
let deepArray = [true, undefined, false, null, [âfreeCodeCampâ]];
JSON changed the undefined to null during the copy. But isn't best practice to define empty data as null rather than undefined? And you probably would be testing if the data was undefined before pushing it on to your array anyways. (Or at least you should be.)
how to copy 2 arrays in to 1 array i have an exam thanks for help
genius!
this is also an alternative: Array.from(original) But it's not supported by IE
you don't need a 0 in original.slice(0) since 0 is the default