Copying Arrays (deep and shallow) - Beau teaches JavaScript

Поділитися
Вставка
  • Опубліковано 1 лис 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.

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

  • @BonzoBouse
    @BonzoBouse 7 років тому +23

    Beau is legit the fastest code typer in history.

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

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

  • @sunilkumarpandab1897
    @sunilkumarpandab1897 4 роки тому

    Very helpful video with detailed explanation

  • @kothuriraogopi8584
    @kothuriraogopi8584 6 років тому +1

    Super and very simple way. Thanks

  • @pam-abhivyakti
    @pam-abhivyakti 3 роки тому

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

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

  • @SisuYeti
    @SisuYeti 4 роки тому

    Thank you for the content.

  • @KyleTower
    @KyleTower 2 роки тому

    Thank you so much!

  • @Human_Evolution-
    @Human_Evolution- 3 роки тому

    Brilliant

  • @kattenelvis1778
    @kattenelvis1778 5 років тому

    THANK YOU

  • @GabrielVasile
    @GabrielVasile 6 років тому

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

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

  • @logixindie
    @logixindie 5 років тому

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

  • @GabrielVasile
    @GabrielVasile 6 років тому

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

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

  • @youssefel-shabasy833
    @youssefel-shabasy833 4 роки тому

    thank you

  • @David-tl2de
    @David-tl2de 10 місяців тому

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

  • @nikytanazarov7711
    @nikytanazarov7711 2 роки тому +1

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

  • @carlnathanmier9451
    @carlnathanmier9451 6 років тому

    genius!

  • @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 6 років тому +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' ] ]

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

  • @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 6 років тому

    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

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