Javascript | Array GroupBy
Wow ! Array has GroupBy too. I am so excited to use this ……
Hello All wonderful developers, hope you had a great Xmas.
Javascript is evolving day by day and it’s becoming my favourite programming language. I am sure many will have the same feeling and many will disagree too.
I came across the array proposals and i thought I should write this out in my medium blog and here it is . It introduces new array methods like array.groupBy and array.groupByToMap . I like the SQL groupBy and I use them and I really love to use it. I am hopping this will help me do the same.
Note : This proposal is currently at stage 3.
This article only covers an example for array.groupBy
Let’s see how to use array.groupBy with an example.
Lets’s start by defining an array
let people = [{ name: "vipin", type: "adult" },{ name: "urmika", type: "kid" },{ name: "david", type: "youth" },{ name: "janvika", type: "kid" },{ name: "mika", type: "adult" },]
Ok. Now the array is ready . So let’s use the array methods on this array and observe the output.
Usage:
const result = people.groupBy(person => {return person.type;});
Output
console.log(result); { 'adult': [ { name: 'vipin', type: 'adult' },
{ name: 'mika', type: 'adult' }, ], 'kid': [ { name: 'urmika', type: 'kid' },
{ name: 'janvika', type: 'kid' } ], 'youth': [ { name: 'david', type: 'youth' } ] }
Wow, The output is great to look at. I am really impressed with this and I hope you all liked it .
Happy new year in advance and Happy javascripting …….. do enjoy programming and your life……
You can buy me a coffee , if you like at https://www.buymeacoffee.com/vipinc007