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

preview_player
Показать описание

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 YouTube series used in the example.

#JS #JavaScript #vanillaJavaScript
Рекомендации по теме
Комментарии
Автор

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 .

AmitK
Автор

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.

shineLouisShine
Автор

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

aengtech
Автор

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.

wertwertwy
Автор

what should be done in order to get the first object in the resultant unique array instead of the last ones
?

Shiva-Rapol
Автор

What’s the effect of using the spread operator in the

let uniqueObjArray = [ … new Map(myObjArray —etc—]

What would happen without that?

misterjaypeasmith
Автор

How to remove duplicates objects in array

sindhurc
Автор

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

fahadhasan
Автор

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

JustClickApprove
Автор

it really help me out thank you too much

swathibaby
Автор

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

santhiya
Автор

How to store a file content in to a object

devika