Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

angular typescript filter array group by attribute

function groupBy(list, keyGetter) {
    const map = new Map();
    list.forEach((item) => {
         const key = keyGetter(item);
         const collection = map.get(key);
         if (!collection) {
             map.set(key, [item]);
         } else {
             collection.push(item);
         }
    });
    return map;
}

// example usage

const pets = [
    {type:"Dog", name:"Spot"},
    {type:"Cat", name:"Tiger"},
    {type:"Dog", name:"Rover"}, 
    {type:"Cat", name:"Leo"}
];
    
const grouped = groupBy(pets, pet => pet.type);
    
console.log(grouped.get("Dog")); // -> [{type:"Dog", name:"Spot"}, {type:"Dog", name:"Rover"}]
console.log(grouped.get("Cat")); // -> [{type:"Cat", name:"Tiger"}, {type:"Cat", name:"Leo"}]
    
    
Comment

PREVIOUS NEXT
Code Example
Typescript :: nodejs express multer s3 
Typescript :: how to put column value counts into a histogram 
Typescript :: remove item from array if exists in another array 
Typescript :: subplots in subplots 
Typescript :: typescript random 
Typescript :: absolute cell reference in excel and google sheets 
Typescript :: react table typescript 
Typescript :: live airplane tracker 
Typescript :: deep partial typescript 
Typescript :: roblox how to weld parts together using script 
Typescript :: check anagramm in typescript 
Typescript :: click within click 
Typescript :: td elements in same line 
Typescript :: type script array declaration 
Typescript :: typescript type number range 
Typescript :: google chrome keyboard shortcuts windows 
Typescript :: tar contents of current folder 
Typescript :: get one property from list of objects linq 
Typescript :: angular rxjs 
Typescript :: compare two lists and remove duplicates java 
Typescript :: How to check if all elements are equal C# 
Typescript :: ignore hosts option in network proxy in ubuntu 16.04 
Typescript :: angular cancel http request 
Typescript :: nest js response timeout 
Typescript :: typescript get objects nested in object 
Typescript :: how to search for elements that are on the webpage using html 
Typescript :: how to read excel spreadsheets in c++ 
Typescript :: open dialog 
Typescript :: react functional components setstate callback 
Typescript :: typeorm transaction example 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =