Copying Arrays (deep and shallow) – Beau teaches JavaScript



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

This Post Has 28 Comments

  1. Keifer

    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 👌

  2. Joseph Cafarelli

    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.

  3. Earl Cornet

    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.

  4. pandit

    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?

  5. Nikyta Nazarov

    It feels like very bad language design. Why don't all javascript objects have a copy method for this

  6. Reehana Qadari

    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 đŸ’ȘđŸ’Ș

  7. Ameer Dhiaa

    deep copy can simply done like
    const copyArray = […originalArray];

  8. Suman Dutta

    you just saved my life i was keep facing the issue for last couple of days…

  9. David

    Beau this is the best explanation of shallow and deep copy I have seen, thanks alot!!

  10. Grahfx

    damn… finally found where is my bug thanks to this video… the way this thing work is completely absurd !

  11. E R

    Huge thank you for that JSON tips

  12. Anurag Bhagsain

    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.

  13. 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.

  14. Logix Indie

    Is using JSON better in performance than manually copying each item for a deep copy?

  15. Robert Kramer

    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.)

  16. Ali N.jaff

    how to copy 2 arrays in to 1 array i have an exam thanks for help

  17. Gabriel Vasile

    this is also an alternative: Array.from(original) But it's not supported by IE

  18. Gabriel Vasile

    you don't need a 0 in original.slice(0) since 0 is the default

Leave a Reply