Copying Arrays (deep and shallow) - Beau teaches JavaScript

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ต.ค. 2024
  • Shallow copy arrays using slice and the spread operator. Deep copy arrays using JSON.stringify.
    🔗 Code: codepen.io/beau...
    🐦 Beau Carnes on Twitter: / carnesbeau
    ⭐JavaScript Playlists⭐
    ▶JavaScript Basics: • JavaScript Basics Course
    ▶Data Structures and Algorithms: • Data Structures and Al...
    ▶Design Patterns: • Design Patterns - Beau...
    ▶ES6: • ES6 - Beau teaches Jav...
    ▶Clean Code: • Clean Code - Beau teac...
    -
    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 freecodecamp.com
    Read great tech articles at medium.freecod...

ความคิดเห็น • 56

  • @steveoliverc
    @steveoliverc 7 ปีที่แล้ว +13

    I really like how you keep the video length short and relative to the topic at hand.

    • @Snesgamer
      @Snesgamer 3 ปีที่แล้ว

      Yup, no extra jokes or introductory filler talk. Yet he manages to also get the best of both worlds by maintaining a friendly and accessible speaking pattern so it still seems fun.

  • @mrcaf2824
    @mrcaf2824 2 ปีที่แล้ว +5

    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.

  • @BonzoBouse
    @BonzoBouse 7 ปีที่แล้ว +23

    Beau is legit the fastest code typer in history.

    • @CianaCorto
      @CianaCorto 5 ปีที่แล้ว +4

      I think it's fast forwarded haha.

    • @manuelcasares7270
      @manuelcasares7270 3 ปีที่แล้ว

      @@CianaCorto it certainly is

  • @VinyciusOliveira
    @VinyciusOliveira 5 ปีที่แล้ว +1

    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.

  • @Hi-373
    @Hi-373 4 ปีที่แล้ว +1

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

  • @SumanDutta1987
    @SumanDutta1987 4 ปีที่แล้ว

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

  • @AnnaMaria-or7se
    @AnnaMaria-or7se 6 ปีที่แล้ว

    Excellent job. I particularly appreciated the part about pointers and arrays & objects within arrays.

  • @anuragbhagsain9724
    @anuragbhagsain9724 5 ปีที่แล้ว +9

    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.

    • @vishalgaware7598
      @vishalgaware7598 2 ปีที่แล้ว

      use the spread operator for copy
      ex: let names2 = [...names];
      names2[0] = {...names2[0], value: 'modified'};
      this would work

  • @shimin_mathew
    @shimin_mathew 6 ปีที่แล้ว +1

    DEEP COPY is incredible idea man !

  • @hananfadah
    @hananfadah 10 หลายเดือนก่อน

    This was so informative. Thanks a lot!

  • @keifer7813
    @keifer7813 2 ปีที่แล้ว

    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 👌

  • @Grahfx
    @Grahfx 4 ปีที่แล้ว

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

  • @er6200
    @er6200 5 ปีที่แล้ว

    Huge thank you for that JSON tips

  • @logixindie
    @logixindie 5 ปีที่แล้ว

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

  • @sunilkumarpandab1897
    @sunilkumarpandab1897 4 ปีที่แล้ว

    Very helpful video with detailed explanation

  • @joaofnds
    @joaofnds 7 ปีที่แล้ว +1

    In terms of performance, worth doing this conversions instead of a recursive deep copy function?

    • @BeauCarnes
      @BeauCarnes 7 ปีที่แล้ว +1

      The way shown in the video is the fastest way according to this: stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript/5344074#5344074

  • @kothuriraogopi8584
    @kothuriraogopi8584 6 ปีที่แล้ว +1

    Super and very simple way. Thanks

  • @pam-abhivyakti
    @pam-abhivyakti 3 ปีที่แล้ว

    That was really helpful, was stuck with this
    Thanx ❤️

  • @KyleTower
    @KyleTower 2 ปีที่แล้ว

    Thank you so much!

  • @earlcornet4606
    @earlcornet4606 2 ปีที่แล้ว +1

    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.

  • @David-tl2de
    @David-tl2de 10 หลายเดือนก่อน

    The issue with deep copy example is that Jason stringify does not convert type functions

  • @SisuYeti
    @SisuYeti 4 ปีที่แล้ว

    Thank you for the content.

  • @Meleeman011
    @Meleeman011 6 ปีที่แล้ว

    what about using map()? i actually have an instance where i'm working with a nodelist of objects in the Dom.

    • @robertkramer5751
      @robertkramer5751 5 ปีที่แล้ว +1

      @@eselse4651 map() creates the same problem with copying the pointer rather than the actual nested array.
      let deepArray = [['freeCodeCamp']];
      let deepCopy = deepArray.map(x => x);
      deepCopy[0].push('is great');
      console.log(deepArray,deepCopy);
      result > [ [ 'freeCodeCamp', 'is great' ] ] [ [ 'freeCodeCamp', 'is great' ] ]

  • @GabrielVasile
    @GabrielVasile 6 ปีที่แล้ว

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

  • @GabrielVasile
    @GabrielVasile 6 ปีที่แล้ว

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

  • @HueJakes
    @HueJakes 7 ปีที่แล้ว

    Hi, should I learn from FCC if I'm interested in becoming an Android developer?

    • @manualvarado2212
      @manualvarado2212 7 ปีที่แล้ว +1

      Not precisely since it doesn't directly teach Android development, but! It will teach you programming and web development, so you will learn fundamentals and beyond, so learning Android development should be easier then :)

  • @nikytanazarov7711
    @nikytanazarov7711 2 ปีที่แล้ว +1

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

  • @reehanq
    @reehanq 2 ปีที่แล้ว

    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 💪💪

  • @Human_Evolution-
    @Human_Evolution- 3 ปีที่แล้ว

    Brilliant

  • @ameerdhiaa5929
    @ameerdhiaa5929 3 ปีที่แล้ว

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

  • @kattenelvis1778
    @kattenelvis1778 5 ปีที่แล้ว

    THANK YOU

  • @youssefel-shabasy833
    @youssefel-shabasy833 4 ปีที่แล้ว

    thank you

  • @joebazooks
    @joebazooks 7 ปีที่แล้ว

    is that codepen you're using

  • @AliNjaff
    @AliNjaff 6 ปีที่แล้ว

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

  • @robertkramer5751
    @robertkramer5751 5 ปีที่แล้ว

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

  • @bettifulop6533
    @bettifulop6533 7 ปีที่แล้ว

    var deepCopy = deepArray;
    Can you explain me, please, why doesn't work this one?

    • @cheeseburger1884
      @cheeseburger1884 7 ปีที่แล้ว +3

      Because you can't copy objects like that. When you declare an array/object like this "var arr_1 = [1,2,3]", arr_1 now points to a place in memory where the array is stored. So when you make arr_2 = arr_1 you basically named the same place in memory with two distinct names.

    • @grandpowr
      @grandpowr 7 ปีที่แล้ว

      Because you would just create another pointer to the same object.

    • @caesarbala
      @caesarbala 6 ปีที่แล้ว

      But even shallowCopy = deepCopy.slice(0) is also reference to same memory right ? that's why its changes made in deepcopy is reflected on shallowcopy too if i am not wrong ? Kindly correct me if so

  • @carlnathanmier9451
    @carlnathanmier9451 6 ปีที่แล้ว

    genius!

  • @RedEyedJedi
    @RedEyedJedi 5 ปีที่แล้ว

    5:47 you're a poet and you didn't know it.

  • @js_programmer8423
    @js_programmer8423 2 ปีที่แล้ว

    his teaching is super confusing ...

  • @joebazooks
    @joebazooks 7 ปีที่แล้ว +4

    your explanations really need some work. there has been a lack of clarity in at least half of your videos that i've watched so far

    • @BeauCarnes
      @BeauCarnes 7 ปีที่แล้ว +4

      What exactly is unclear? I would definitely appreciate more specific criticism so I know what to work on. Thanks!

    • @edmilinski1295
      @edmilinski1295 7 ปีที่แล้ว +6

      rubbish, both explanations and attitude are excellent.
      best javascript videos I've seen. good reminder, refresher for anyone -)

    • @joebazooks
      @joebazooks 7 ปีที่แล้ว

      next time i encounter the same feeling i'll be more specific.

    • @carlnathanmier9451
      @carlnathanmier9451 6 ปีที่แล้ว

      i no englis speaker, but i understood wel