# map-filter

> Double loops suck.

Latest version **1.0.0** (published 2017-03-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install map-filter
pnpm add map-filter
yarn add map-filter
bun add map-filter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-03-29 |
| First published | 2017-03-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Alex Nadalin |
| Maintainers | odino |
| Keywords | map, filter, reduce, swag |

## Links

- npm: https://www.npmjs.com/package/map-filter
- Repository: https://github.com/odino/js-map-filter
- Homepage: https://github.com/odino/js-map-filter#readme
- Issues: https://github.com/odino/js-map-filter/issues
- npm.io page: https://npm.io/package/map-filter

## Recent versions

- 1.0.0 (latest) — 2017-03-29

## README

# map-filter

[![Build Status](https://travis-ci.org/odino/js-map-filter.svg?branch=master)](https://travis-ci.org/odino/js-map-filter)

When you need to run `map` and `filter` together (*aka elegant reduce*).

## Why

I've seen code that looks like:

``` js
let list = [
  {name: 'alex', age: 28},
  {name: 'frank', age: 28},
  {name: 'joe', age: 30},
  {name: 'ayham', age: 28},
  {name: 'solomon', age: 29},
]

list.filter(p => p.age < 30).map(p => {name: p.name}) // youngsters!
```

Whereas you can achieve much better performance with a simple
`reduce`:

``` js
list.reduce((acc, p) => {
  if (p.age < 30) {
    acc.push({name: p.name})
  }

  return acc
}, [])
```

No trust? Check yourself:

```
~/projects/js-map-filter (master ✔) ᐅ node bench.js                                                                                                                                                                                1s
filter + map: 19.378ms
reduce: 6.254ms
map-filter module: 7.116ms
```

## So what does this guy do?

This utility lets you do the same things you'd do with a reduce,
without having to keep track of the accumulator:

``` js
const mapFilter = require('map-filter')

filterMap(list, p => {
  if (p.age < 30) {
    return {name: p.name}
  }
})
```

Just return `undefined` (*aka don't return*) for the values you
want to exclude from the final list.

## Tests

Just `mocha` it:

```
~/projects/js-map-filter (master ✘)✹✭ ᐅ mocha                                                                                                                                                                                      1s


  mapFilter
    ✓ should be able to return all original values if no fn is specified
    ✓ should be able to exclude values
    ✓ should be able to map values
    ✓ should be able to handle falsey values


  4 passing (9ms)
```

---
_Source: https://npm.io/package/map-filter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
