Skip to content
/ map-sift Public

Perform JavaScript map and filter operations in the same iteration

License

Notifications You must be signed in to change notification settings

mpoc/map-sift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mapSift

Perform map and filter operations in the same iteration

Install

npm install map-sift

Usage

import mapSift from 'map-sift'

type Person = { name: string, height?: number }

const people: Person[] = [
    { name: 'Alice', height: 180 },
    { name: 'Bob', height: 168 },
    { name: 'David', height: undefined },
    { name: 'Emma', height: 160 },
    { name: 'Frank' },
    { name: 'Grace', height: 155 },
]

// Without mapSift:
const heightsMapFilter = people
    .map(person => person.height)
    .filter(Boolean)
console.log(heightsMapFilter) // [180, 168, 160, 155]

// With mapSift:
const heightsMapSift = people
    .flatMap(mapSift(person => person.height))
console.log(heightsMapSift) // [180, 168, 160, 155]

About

Perform JavaScript map and filter operations in the same iteration

Resources

License

Stars

Watchers

Forks

Packages

No packages published