Copying Arrays (deep and shallow) - Beau teaches JavaScript

Поделиться
HTML-код
  • Опубликовано: 21 окт 2024

Комментарии • 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.

  • @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...

  • @shimin_mathew
    @shimin_mathew 6 лет назад +1

    DEEP COPY is incredible idea man !

  • @AnnaMaria-or7se
    @AnnaMaria-or7se 6 лет назад

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

  • @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 👌

  • @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

  • @hananfadah
    @hananfadah 10 месяцев назад

    This was so informative. Thanks a lot!

  • @er6200
    @er6200 5 лет назад

    Huge thank you for that JSON tips

  • @sunilkumarpandab1897
    @sunilkumarpandab1897 4 года назад

    Very helpful video with detailed explanation

  • @kothuriraogopi8584
    @kothuriraogopi8584 6 лет назад +1

    Super and very simple way. Thanks

  • @Grahfx
    @Grahfx 4 года назад

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

  • @pam-abhivyakti
    @pam-abhivyakti 3 года назад

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

  • @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

  • @David-tl2de
    @David-tl2de 10 месяцев назад

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

  • @logixindie
    @logixindie 5 лет назад

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

  • @nikytanazarov7711
    @nikytanazarov7711 2 года назад +1

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

  • @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.

  • @GabrielVasile
    @GabrielVasile 6 лет назад

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

  • @SisuYeti
    @SisuYeti 4 года назад

    Thank you for the content.

  • @GabrielVasile
    @GabrielVasile 6 лет назад

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

  • @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' ] ]

  • @KyleTower
    @KyleTower 2 года назад

    Thank you so much!

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

  • @ameerdhiaa5929
    @ameerdhiaa5929 3 года назад

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

  • @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 :)

  • @AliNjaff
    @AliNjaff 6 лет назад

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

  • @joebazooks
    @joebazooks 7 лет назад

    is that codepen you're using

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

  • @kattenelvis1778
    @kattenelvis1778 5 лет назад

    THANK YOU

  • @youssefel-shabasy833
    @youssefel-shabasy833 4 года назад

    thank you

  • @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

  • @Human_Evolution-
    @Human_Evolution- 3 года назад

    Brilliant

  • @RedEyedJedi
    @RedEyedJedi 5 лет назад

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

  • @carlnathanmier9451
    @carlnathanmier9451 6 лет назад

    genius!

  • @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