20+ Must Know Array Methods That Almost Nobody Knows

Поділитися
Вставка
  • Опубліковано 31 лип 2024
  • We are all used to array methods like .map, .forEach, etc, but that is just the tip of the iceberg when it comes to useful array methods. In this video I cover over 20 amazing new array methods that you have probably never heard of.
    📚 Materials/References:
    Sets Video: • Another 5 Must Know Ja...
    Sets Article: blog.webdevsimplified.com/202...
    🌎 Find Me Here:
    My Blog: blog.webdevsimplified.com
    My Courses: courses.webdevsimplified.com
    Patreon: / webdevsimplified
    Twitter: / devsimplified
    Discord: / discord
    GitHub: github.com/WebDevSimplified
    CodePen: codepen.io/WebDevSimplified
    ⏱️ Timestamps:
    00:00 - Introduction
    00:27 - #1
    02:15 - #2-3
    03:16 - #4
    04:11 - #5
    04:38 - #6
    05:11 - #7-9
    06:18 - #10-11
    07:35 - #12-13
    09:00 - #14
    09:50 - #15-21
    #ArrayMethods #WDS #JavaScript

КОМЕНТАРІ • 155

  • @FunctionGermany
    @FunctionGermany 7 місяців тому +220

    flatMap is NOT like .flat() .map(), it's like .map() .flat(), very different!!

    • @WebDevSimplified
      @WebDevSimplified  7 місяців тому +61

      Good point. I messed up when talking about this.

    • @ehm-wg8pd
      @ehm-wg8pd 7 місяців тому +48

      they should call it matFlap

    • @xiaozhenxiang
      @xiaozhenxiang 7 місяців тому +13

      ​@@ehm-wg8pdflatMap = flat the map

    • @JokeryEU
      @JokeryEU 7 місяців тому +4

      also flatMap only goes 1 lvl deep

    • @VladBurlutsky
      @VladBurlutsky 7 місяців тому +3

      Always read the ECMAScript specs, that's what all hardcore JS programmers use!
      The Array.prototype.flatMap and Array.prototype.flat methods in JavaScript are quite similar in their functionality, but they have some key differences:
      Functionality:
      flatMap: This method first maps each element using a mapping function, then flattens the result into a new array. It's essentially a combination of the map and flat methods.
      flat: This method simply flattens an array up to a specified depth and does not perform any mapping.
      Mapping Function (mapperFunction):
      flatMap: Requires a mapperFunction as its first argument. This function is called on every element of the array, and the results are then flattened.
      flat: Does not take a mapping function. It only flattens the array based on the specified depth.
      Depth Specification:
      flatMap: Implicitly flattens the array only one level deep. It does not accept a depth argument.
      flat: Accepts an optional depth argument that determines the depth level to which the array should be flattened. If depth is not provided, the default is one level deep.
      Error Handling (TypeError):
      flatMap: Throws a TypeError if the provided mapperFunction is not callable.
      flat: There's no such error condition specifically related to the function's argument since it doesn't require a function as an argument.
      Depth Handling in flat:
      flat: Contains additional steps to handle the depth parameter. If depth is undefined, it is treated as 1, and if it is less than 0 after conversion to an integer, it is set to 0.@@WebDevSimplified

  • @user-je5kv3uh2t
    @user-je5kv3uh2t 7 місяців тому +7

    Group by & differences were totally new to me. Thanks for the video mate

  • @sarveshdhiman15
    @sarveshdhiman15 5 місяців тому +1

    In a single video I learn lot of things with proper explanation and with so ease. Thanks 🙏

  • @davidj6755
    @davidj6755 7 місяців тому +21

    I’m excited about the set theory operators. I hope that eventually gets full support across major browsers.

    • @tropicaljupiter
      @tropicaljupiter 7 місяців тому

      Every day we stray further from lodash’s light

  • @psydook
    @psydook 7 місяців тому

    been waiting for this forever. Thank you man

  • @quanghungpham269
    @quanghungpham269 7 місяців тому +3

    I used most of these method quite frequently, they are super useful. My fav one is reduce(), it's very useful when you want to reduce the array to a value and then use that value, also I got headache many times from using it :)

  • @ahmadfahim6467
    @ahmadfahim6467 7 місяців тому

    Thanks man, especially the flapmap and explaining array mutations

  • @harmez7
    @harmez7 7 місяців тому +2

    Kyle, thank you for speaking so clearly.
    Im not that good at English but I can completely understand you, that's why I subbed to here years ago.

    • @rdubb77
      @rdubb77 5 місяців тому

      It’s true, his pacing and diction is very good.

  • @user-tw6tr8rb6n
    @user-tw6tr8rb6n 7 місяців тому +1

    Bro you really helped a lot. Thank you!

  • @NiceChange
    @NiceChange 6 місяців тому

    Great video. Kyle makes JS fun and interesting...not an easy thing to do. 😊

  • @aundefined
    @aundefined 7 місяців тому

    Great video! Keep it up 💯

  • @magnanova
    @magnanova 7 місяців тому +3

    It's about time they added those Set difference/intersection methods!

  • @catto88
    @catto88 7 місяців тому

    Wow, didn't knew about .with (this one is very useful!), .groupBy, start/end arguments of .fill, also didn't knew you can add argument like depth to .flat and about Set method, tysm!

  • @rajas_world_20
    @rajas_world_20 12 днів тому

    Thanks Kyle the video is very informative

  • @ajedamilola8146
    @ajedamilola8146 7 місяців тому

    I wish I had seen this video sooner. This groupBy would have saved me lots of time. I had to do this manually.

  • @industrydzina
    @industrydzina 7 місяців тому +7

    Important to note that the .fill method behaves differently when you fill the array with objects(including arrays), where it actually fills the array with a REFERENCE to the original object, so that if you perform a mutation on any of the objects in this array, it will perform the same mutation on all the elements in the array. Was stumped by this until ChatGPT helped point me to the docs on MDN...

    • @Ebiko
      @Ebiko 7 місяців тому +1

      That's why fill is regularly used with a lambda to generate unique objects.

  • @felixkiptoo421
    @felixkiptoo421 7 місяців тому

    You really did simplified my flatMap() understanding

  • @kuhaniresti
    @kuhaniresti 7 місяців тому

    hey, did it come into your mind of building hair gel products lately? great tutorials always

  • @jischkebd
    @jischkebd 7 місяців тому +2

    you explain these concepts better than the majority of the teachers out there!

  • @roebucksruin
    @roebucksruin 7 місяців тому +1

    An important distinction of .fill(), it fills the array with a static value. If you’re filling with an object, it’s the exact same object in memory. I tried to use it with the array constructor to convert an integer into an array of objects with a length of said integer, and every index would change on edit.

  • @ukaszzbrozek6470
    @ukaszzbrozek6470 7 місяців тому +2

    I know them all except the set methods. Thanks for a great reminder.
    When using shift and unshift we should remember that is a very non-performant action because the array need to recalculate all indexes. We should avoid using them.
    To be honest knowing about at method didn’t change that I am still using array[array.length - 1] syntax. It is such strong muscle memory that I write that without thinking. XD

    • @ryanlinnane
      @ryanlinnane 7 місяців тому

      The shift methods are good for interviews for quickly representing a deck.

  • @shgysk8zer0
    @shgysk8zer0 7 місяців тому +1

    You had me a bit concerned including groupBy() and the Set methods... I was worried the spec had changed yet again.
    Yes, I knew all of them. I've written polyfills for them, which is why I was worried specs had changed.

  • @0luckyluca0
    @0luckyluca0 7 місяців тому

    amazing timestaps, i always wanted to skip to the topic "6" :D

  • @ryan.wakefield
    @ryan.wakefield 7 місяців тому +4

    The one thing I felt like was missing from this video is that you usually make sure to provide example situations in when you might use the code, and more importantly why. Otherwise, learned a lot of cool new stuff I will try to find a use for. Thanks!

  • @yellowbonbon1
    @yellowbonbon1 7 місяців тому

    Thank you for this sharing very helpful. Just want to add this, toReversed, toSorted and toSpliced create a new array. However, they do not do deep clone. In some use case, React could not detect the change if the change is within an object of object. It will be better to note this. "create a new array" somehow could cause confusion.

  • @GuildOfCalamity
    @GuildOfCalamity 7 місяців тому +1

    Kyle's hair should have its own channel.

  • @DarrenJohn10X
    @DarrenJohn10X 7 місяців тому

    .at() got all the excited press, meanwhile .with() was virtually ignored.
    Thanks Kyle for talking about both together.

  • @dave6012
    @dave6012 7 місяців тому

    Shift and unshift are how I do BFS when writing algos in JS

  • @MichaelCampbell01
    @MichaelCampbell01 7 місяців тому

    A fun exercise is to implement all these methods in terms of `reduce()`.

  • @BokoMoko65
    @BokoMoko65 7 місяців тому +1

    Curiously, it's note supported in Node.js. Although it's very easy to implement
    function groupBy(list, keyGetter) {
    return list.reduce((ac, curr) => {
    const key = keyGetter(curr);
    ac[key] = ac[key] || [];
    ac[key].push(curr);
    return ac;
    }, {});
    }

    • @michaelharrington5860
      @michaelharrington5860 7 місяців тому

      Alternative:
      function groupBy(array, key) {
      return array.reduce((groupedItems, item) => {
      const keyValue = item[key];
      if (!groupedItems[keyValue]) {
      groupedItems[keyValue] = [];
      }
      groupedItems[keyValue].push(item);
      return groupedItems;
      }, {});
      }
      let groupedByName = groupBy(people, 'name');
      console.log(groupedByName);

  • @aliampichelmilan3345
    @aliampichelmilan3345 7 місяців тому

    Nice vidio crack!, greetings!

  • @raygan3
    @raygan3 7 місяців тому

    Will it be automatically polyfilles if used eq in react?

  • @cm3462
    @cm3462 7 місяців тому

    Fantastic

  • @meqdadmoradi
    @meqdadmoradi 7 місяців тому

    Please make a video about blobs and array buffer

  • @daedalus5070
    @daedalus5070 7 місяців тому +6

    Prime made a comment recently about some of these and I kind of agree... we need a toMap, toFilter that does mutate the original array. Just to keep JS consistently inconsistent. 😉

  • @sureshanandsekar4384
    @sureshanandsekar4384 7 місяців тому

    sir could you please tell me where is the link for that cheet sheet , am ur new subscriber . you are my guru. and getting each time inspired by ur video ....

  • @user-mw4jj6vj4r
    @user-mw4jj6vj4r 7 місяців тому

    Hi everyone, i have node js version 21, but i cant use this methods in vscode. How can i enable this methods?

  • @Thassalocracy
    @Thassalocracy 7 місяців тому +15

    It's unfortunate that a lot of these methods are not used more due to the predominance of React in the JavaScript world. React wants to go the way of purely functional programming, so mutating functions like pop, shift, unshift are a big no-no. But if somehow JavaScript could be used in data science then these methods could really come in handy. But first, someone has to come up with handy data science packages like numpy, pandas, scikit 😅
    PS: I actually did post a comment earlier but deleted it because I made some mistakes and didn't want to create unneeded controversy 😅

    • @daleryanaldover6545
      @daleryanaldover6545 7 місяців тому +3

      good thing we don't use react in our projects

    • @user-ck7ts3ml1g
      @user-ck7ts3ml1g 7 місяців тому

      @@daleryanaldover6545 What is used?

    • @sethwright2518
      @sethwright2518 7 місяців тому +2

      These new functions (most of them at least) are actually good for React specifically because they don't mutate the originals arrays.. so it works better for functional programming 🎉

  • @BokoMoko65
    @BokoMoko65 7 місяців тому

    the with method is kind of superfluous because we can already use the destructuring technique. Like, putting something at the beginning
    let inTheBeginning = [ newThing, ... people]
    let inTheEnd = [ ... people, newThing]
    But if the idea is to insert some value somewhere in the middle of the array, the with is handy indeed

  • @cadekachelmeier7251
    @cadekachelmeier7251 7 місяців тому

    I use .fill when I just want to map over a range of numbers. Mapping over new Array(x) doesn't work since there aren't any keys. But you can do
    new Array(x).fill(0).map((val, index) => index)

    • @whosdr
      @whosdr 7 місяців тому +1

      Array.from({length:x}) or [...Array(x)] are also viable solutions.

  • @vichikes
    @vichikes 7 місяців тому

    Groupby is super useful

  • @leandrohendrix6487
    @leandrohendrix6487 7 місяців тому

    I can't use 'with method' in NodeJs. Should I Install some nodeJs package for use it?

  • @codeguy11
    @codeguy11 7 місяців тому +1

    "20+ Must Know Array Methods That Almost Nobody Knows": 💀
    Me using VSCode's autocomplete (and sometimes MDN docs) to browse and explore all JavaScript APIs and features: *Look what they need to mimic a fraction of our power*

  • @amitd1927
    @amitd1927 7 місяців тому

    How about performance of each function

  • @andrillaf
    @andrillaf 7 місяців тому

    It’s good to be a developer on Mac😎

  • @vergil_389
    @vergil_389 7 місяців тому

    Hello Kyle.
    Will you avail your course on offer (on a discounted rate) for Christmas. or Before Christmas. 🤞🤞🤞

  • @ahmed37551
    @ahmed37551 7 місяців тому

    i think you have done enough intros kyle. just keep in the bio.

  • @shivan2418
    @shivan2418 7 місяців тому

    I use lodash for most of this functionality.

  • @nidhijha7081
    @nidhijha7081 7 місяців тому

    Only if i would have watched this video one day before, my interview could have been much better 😢

  • @stanmarsh9458
    @stanmarsh9458 7 місяців тому

    groupBy is top!

  • @VisualArtRonny
    @VisualArtRonny 6 місяців тому

    Apparently my face is buried inside MDN docs, since I knew all of them including the last Set methods

  • @BokoMoko65
    @BokoMoko65 7 місяців тому

    Is there a method to "flat" an object? That is, to make a deep copy of the entire object made of other objects?

    • @benas989989
      @benas989989 7 місяців тому +2

      Old way:
      const copy = JSON.parse(JSON.stringify(foo))
      New way:
      const copy = structuredClone(foo)

  • @mecozir
    @mecozir 7 місяців тому

    part yield function set key specify task

  • @SamuelKarani
    @SamuelKarani 7 місяців тому

    I think you made an error equating flatMap with flat(infinity).map. I agree with @FunctionGermany in the comments

  • @prafullkumar6369
    @prafullkumar6369 7 місяців тому

    amazon-chime-sdk-component-library-react Please create a video on this

  • @cbbcbb6803
    @cbbcbb6803 6 місяців тому

    What did people do when there was just array?

  • @sleeplessowl777
    @sleeplessowl777 7 місяців тому

    array instanceof Array

  • @21Kikoshi
    @21Kikoshi 7 місяців тому

    As a youtuber i wonder if youtubers ever watch themselves at x2 or with no sound to see what its like

  • @DarrenJohn10X
    @DarrenJohn10X 7 місяців тому

    .fill() ignores in sparse arrays... just so folks are aware! So you can't build an array of 5 "*" with this: let a = Array(5).fill("*")
    You would need to first map to a non-undefined value every element you want star-ified.

  • @amuuuinjured
    @amuuuinjured 7 місяців тому

    knew all of them except last experimental methods...

  • @krzysztofrozbicki1776
    @krzysztofrozbicki1776 7 місяців тому +1

    The main question is - are they faster than simple for() loop ?
    I have been playing with the js loops lately and it seems that for array of 10 millions random numbers the for() loop is much faster than: filter, find, forEach, map and reduce.
    And by faster i mean like 10x times faster.
    The only method that was faster was the build in sort() method. That worked better even then insertion sort/ merge sort/ or quick sort Algorithm made in JS for loop - i am not sure but i suspect this is because of V8 chromium engine which uses C++

    • @catalintudorciurte309
      @catalintudorciurte309 7 місяців тому

      There are some things to unpack here. A for loop is inlined, while the methods call the given function every single time. Also, a for loop gives you the oportunity to mutate the old array, while most of the other functions create a new array every single time

  • @skywizard3319
    @skywizard3319 5 місяців тому

    I would really love if you started naming the chapters instead of numbering them

  • @user-ik7rp8qz5g
    @user-ik7rp8qz5g 7 місяців тому

    Having reduceRight makes it sound like default reduce is wrong

  • @michaelharrington5860
    @michaelharrington5860 7 місяців тому

    `groupBy` alternative for node.js users:
    function groupBy(array, key) {
    return array.reduce((groupedItems, item) => {
    const keyValue = item[key];
    if (!groupedItems[keyValue]) {
    groupedItems[keyValue] = [];
    }
    groupedItems[keyValue].push(item);
    return groupedItems;
    }, {});
    }
    let groupedByName = groupBy(people, 'name');
    console.log(groupedByName);

  • @BauldyBoys
    @BauldyBoys 7 місяців тому

    CSS starts calling everything start and end Javascript starts using left and right. Classic.

    • @chrisjohanesen
      @chrisjohanesen 7 місяців тому

      And then there’s “findLast” 🙄

  • @Pareshbpatel
    @Pareshbpatel 7 місяців тому

    Nice one, Kyle. Thanks.
    {2023-12-14}

  • @LetrixAR
    @LetrixAR 7 місяців тому

    The namings for some of these methods are really confusing

  • @vaishnomatabhaktamandali2000
    @vaishnomatabhaktamandali2000 7 місяців тому

    In bathroom rn but listening to lectures

  • @bronzekoala9141
    @bronzekoala9141 7 місяців тому

    Nice, but who th thought "unshift" was a good name for that???

  • @nomadshiba
    @nomadshiba 7 місяців тому

    epic

  • @LawJolla
    @LawJolla 7 місяців тому

    This video taught me that someone still uses a PC. Wild.

  • @shirakuyanai4095
    @shirakuyanai4095 7 місяців тому +1

    First time here within 1 minute.

  • @turolretar
    @turolretar 7 місяців тому

    “Let’s see Paul Allen’s array methods”

  • @mecozir
    @mecozir 7 місяців тому

    wait general fixed job dom

  • @jasonlough6640
    @jasonlough6640 7 місяців тому +3

    There should be an opposite to .flat().
    flat() destroys the structure. What if we were to specify a structure and give an array, like:
    Array.inflate(arr, structure)
    where
    arr = [5,11,87,2]
    structure = ['age', weight']
    which outputs
    [
    { age: 5, weight: 11},
    { age: 87, weight: 2},
    }

    • @whosdr
      @whosdr 7 місяців тому +2

      function inflate( values, structure ) {
      return Object.values(
      Object.groupBy( values, (e,i)=>Math.floor( i/structure.length ) )
      ).map( e => Object.fromEntries(
      e.map( (v,i) => [structure[i],v] )
      )
      );
      }

  • @Anonymous____________A721
    @Anonymous____________A721 7 місяців тому +1

    388th view

  • @piyushaggarwal5207
    @piyushaggarwal5207 7 місяців тому +5

    Personal Notes, don't see - Object.groupBy(), Map.groupBy(), Array.with(1, 2), Array.at(-1), Array.fill(1, 1, 3), Array.toReversed(), Array.toSorted(), Array.toSpliced(), Array.flat(2), Array.findLast(), Array.reduceRight()

    • @SoreBrain
      @SoreBrain 7 місяців тому +1

      Saved me some time, ty

    • @piyushaggarwal5207
      @piyushaggarwal5207 7 місяців тому

      @@SoreBrain I have written down according to me. Skipping things.

    • @SoreBrain
      @SoreBrain 7 місяців тому +1

      @@piyushaggarwal5207 I realized you left some out, coincidentally the ones I also don't care about (yet) or already knew well. Thanks for the edit and letting me know tho!

  • @mangadi3859
    @mangadi3859 7 місяців тому

    oh sh- i have to change my .sort() to .toSorted()?

  • @CoryTheSimmons
    @CoryTheSimmons 7 місяців тому +2

    reduce is king. Everything else is just a lodash helper.

  • @foxcryptoboss
    @foxcryptoboss 7 місяців тому

    something is very off with the camera and sound sync. looks scary.

  • @olegkravchenko9655
    @olegkravchenko9655 7 місяців тому

    С*ка, до слёз! Даже плюсик поставил от нахлыва чуйств :D

  • @vijaykv555
    @vijaykv555 7 місяців тому +3

    First View

  • @akshaykommu4524
    @akshaykommu4524 7 місяців тому +1

    2nd view

  • @bryson2662
    @bryson2662 7 місяців тому

    Most shocking referral, Kyle uses windows

  • @genyklemberg
    @genyklemberg 7 місяців тому

    Who cares what you are talking about, I already heard about it and still don't use it 😢 I think I need to visit coding interviews to practice that more often😂

  • @AGSTRANGERTunisianAuthor
    @AGSTRANGERTunisianAuthor 7 місяців тому +40

    If almost nobody knows them then probably you don't need to know them

    • @yadusolparterre
      @yadusolparterre 7 місяців тому +12

      How else am I going to flex in front of my colleagues

    • @vodafoneuser1690
      @vodafoneuser1690 7 місяців тому +2

      As demonstrated in this video lol

    • @angrytvrobot6130
      @angrytvrobot6130 7 місяців тому +6

      I'm sure there will be a few people who see this and go, "oh that will make my bit of code much easier!" There's probably a use case, or they wouldn't exist.

    • @TaufeeqAhmed-fq9hq
      @TaufeeqAhmed-fq9hq 7 місяців тому

      Nice motto 👏👏

    • @VskiDevs
      @VskiDevs 7 місяців тому +1

      He said they’re just as useful 🫴🏿

  • @montebont
    @montebont 7 місяців тому +1

    Solution looking for a problem...

  • @mecozir
    @mecozir 7 місяців тому

    post none if code expire

  • @sevgiligencler
    @sevgiligencler 7 місяців тому

    Very noob

  • @ravisankarp61
    @ravisankarp61 7 місяців тому

    Please explain a bit slowly, you are explaining like you are running a sprint.

    • @windubitably
      @windubitably 7 місяців тому

      You can slow the video down. I think his pacing is great.

  • @user-xf8mc8bd2u
    @user-xf8mc8bd2u 7 місяців тому

    Is there a new way to get the last item in the array instead of the old array[array.length - 1]?

    • @Popo-pd3ps
      @Popo-pd3ps 7 місяців тому

      Just simply use array.at(-1)

  • @dasten123
    @dasten123 7 місяців тому

    I really don't see the point of `array.at`.. even for negative indexes, I would prefer `array[array.length - x]` or am I too old-fashioned? :o

    • @soniablanche5672
      @soniablanche5672 7 місяців тому +2

      it saves you a line of code to store the array in a variable
      example:
      const array = getArrayFromApiCall();
      array[array.length - 1]
      vs
      getArrayFromApiCall().at(-1);

    • @dasten123
      @dasten123 7 місяців тому

      @@soniablanche5672 Ahh!! Yes that makes sense! Thank you, I feel so much better now, haha :D

  • @mecozir
    @mecozir 7 місяців тому

    wait general fixed job dom