Get a Unique List of Objects in an Array of Object in JavaScript

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ก.ค. 2024
  • To the tutorial! : yagisanatode.com/2021/07/03/g...
    00:00 Intro
    00:38 Build the array of objects
    03:36 Write the one-liner
    07:17 Alternate example
    08:28 the map() method
    10:52 Map object
    14:17 Map object key() method
    16:42 Map object values() method
    17:30 spread syntax
    19:08 summary
    Learn a 'unique' way (pun intended) to get a distinct list of objects in an array of objects based on a shared key in JavaScript.
    Bonus points if you know the name of the TH-cam series used in the example.
    #JS #JavaScript #vanillaJavaScript

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

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

    Wow, what an tutorial, which I ran into completely by an accident.
    I wasn't expected it, and it is amazingly helpful.
    I'm very impressed!
    Thanks a lot :))
    P.S.
    Just read your article after following the link at the description.
    It clarify few things.
    Awesome.

  • @JustClickApprove92
    @JustClickApprove92 2 ปีที่แล้ว +8

    Instead of copying and pasting, you can highlight the code you want to copy and press alt + shift + down arrow

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

      For sure. Great shortcut. Not too sure how universal the shortcut is with other editors, so stayed with copy and paste to keep it safe.

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

      great

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

    Awesome !! now tell me , if this is possible in this example below:
    [
    {
    userId: 1,
    joinTime: 1234
    },
    {
    userId: 2,
    joinTime: 1234
    },
    {
    userId: 1,
    joinTime: 1235
    },
    {
    userId: 2,
    joinTime: 1236
    },
    {
    userId: 1,
    joinTime: 1237
    }]
    I need to get only unique userId's but with highest number of joinTime
    e.g. [
    { userId: 1, joinTime: 1237 },
    { userId: 2, joinTime: 1236 }
    }
    can we somehow conditionally set new Map to select unique only where joinTime is higher, or may be , we first put all objects of the array in sorting order from lower joinTime to high joinTime and then we put it in new Map .

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

      You would probably be better off using the reduce() method in your case and storing the array there. On each iteration, you could check the accumulator of your array against your item to ensure that it is the one with the highest join time.

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

    it really help me out thank you too much

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

    What’s the effect of using the spread operator in the
    let uniqueObjArray = [ … new Map(myObjArray -etc-]
    What would happen without that?

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

      Without the spread operator, you would just create a single mapped object inside an array " [map]". The spread operator iterates through the map breaking each part out to return it back to an array of objects. Hope this helps.

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

    Thank you. I know how the new Set method work but it gives only unique value not the entire array

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

    typo on your website "spend some to"
    But as many code oneliners are, they are a little tricky to understand. So I wanted to spend some to understanding how this worked and thought, you might find this a little interesting too.

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

      Great find, Jesse. All updated now.

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

    How to remove duplicates objects in array

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

    I need one key value from one object.how to do that .my question how to get a particular key vale from object

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

      Hi Santhiya, to get an value from a key you can use myObj.keyName or myObj['keyName']. You can check if a key exists in an object with myObj.hasOwnProperty('keyName'). Apologies if this doesn't answer your question.

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

      @@yagisanatode thank you

    • @inainthakaikal4522
      @inainthakaikal4522 ปีที่แล้ว

      Hi, santhiya! Do you have got an answer?
      Please tell me

  • @fahadhasan1436
    @fahadhasan1436 ปีที่แล้ว

    I think we can also use Set in place of Map to get unique objects am I correct?

    • @yagisanatode
      @yagisanatode  ปีที่แล้ว

      Hi Fahad, I haven't found a good way to do this that would a) allow me to choose which property in each object that I want to check is unique across all objects. b) return the entire object for each unique item in the array. c) is a neat one-liner.
      Would love to see a good Set-based solution for this 👍.

  • @funnyanimalworld7579
    @funnyanimalworld7579 ปีที่แล้ว

    what is this Map with capital M?

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

      Yeah. It is an unusual one. The Map constructor allows you to create map objects. At the sake of enraging the neck-beards, the simple difference is that Maps can be ordered and keys and values can be in any data type. This makes them a little more performant when you need an object that you will add and remove to frequently. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/Map medium.com/front-end-weekly/es6-map-vs-object-what-and-when-b80621932373

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

    I want number of people.. how to get?

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

      In the example we are using an object array of people so all you need to do is use the 'length' method >> myObjArray.length

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

    How to store a file content in to a object

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

      That's a tricky one and depends on a lot of factors. For example, what platform are you reading the file from, browser, Node.js, Apps Script? What is the file type? What type of data are you trying to extract and insert?
      Add you iterate over your data you can add it to your Object in a number of ways: developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics

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

      @@yagisanatode i want to covert the file content to store or add object and extract the number of content in file and another summary.. like student details in file content that read and store it to object.. how that possible?? I used in nodejs. File is text file..

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

      A common approach in Node.js would be to use the fs module with the readFile() method. nodejs.org/en/knowledge/file-system/how-to-read-files-in-nodejs/
      You would need to use regular expressions to match the areas that you want to extract from the files. javascript.plainenglish.io/learn-to-use-regular-expressions-like-a-ninja-in-node-js-20cfb6806f26