JSPM

indexed-filter

1.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 25109
  • Score
    100M100P100Q140538F
  • License ISC

Array#filter() with also detecting indexes of filtered values

Package Exports

  • indexed-filter

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (indexed-filter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

indexed-filter

npm version Build Status Coverage Status

Array#filter() with also appending indexes of filtered values to the result

const arr = [1, 'foo', 2, 'bar'];

arr.filter(v => typeof v === 'string');
//=> ['foo', 'bar']

indexedFilter(arr, v => typeof v === 'string');
//=> [{index: 1, value: 'foo'}, {index: 3, value: 'bar'}]

Installation

Use npm.

npm install indexed-filter

API

import indexedFilter from 'indexed-filter';

indexedFilter(array, filterFn [, thisObject])

array: Array
filterFn: Function
thisObject: (any value)
Return: Array

The API is very similar to Array.prototype.filter(). There are only two differences:

  • You pass in the array as the first argument instead of calling the .filter() method on the array instance.
  • Each filtered result is an object with tow properties, index (array index integer) and value (array element).
indexedFilter([0, [1], [2], '3', [5]], function(val, index, arr) {
  return this.isArray(val) && index % 2 === 0 && index < arr.length * 0.5;
}, Array);
//=> [{index: 2, value: [2]}]

License

ISC License © 2018 Shinnosuke Watanabe