mapsome

2018-10-23 admin

mapsome是什么

什么是mapsome,find maped element of an array that will satisfy condition of some

mapsome使用教程帮助文档

Mapsome License NPM version Dependency Status Build Status Coverage

Find maped element of an array that will satisfy condition of some.

Map + Some + early return of an element

Install

npm i mapsome --save

How come?

Lets look at regular map-filter example:

const map = (a) => ++a;
const condition = (a) => a > 2;
[1, 2, 3, 4]
    .map(map)           /* loop full array */
    .filter(condition)  /* loop full array again */
    .pop();             /* get value */
// returns
3

With mapsome this would look this way:

const map = (a) => ++a;
const condition = (a) => a > 2;
mapsome(map, condition, [1, 2, 3, 4]);
// returns
3

mapsome works much faster because no need map full array, first satisfied condition will break the loop.

API

mapsome(map [, condition ], array)

  • mapping function
  • condition of stopping the loop
  • array

mapsome could be used with map function only:

const map = (a) => ++a;
mapsome(map, [0, 0, 3, 0]);
// returns
3
// same as
mapsome(map, a => a, [0, 0, 3, 0]);
// returns
3

Environments

Node.js

In old node.js environments that supports es5 only, mapsome could be used with:

var mapsome = require('mapsome/legacy');

Web Browser

mapsome could be installed via bower with:

bower install mapsome --save

When loaded in browser mapsome uses global variable with same name (when browserify or webpack did not used).

License

MIT

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-43442.html

文章标题:mapsome

回到顶部