indexed-filter
Array#filter
with also detecting indexes of filtered values
const arr = [1, 'foo', 2, 'bar'];
arr.filter(v => typeof v === 'string');
indexedFilter(arr, v => typeof v === 'string');
Installation
Package managers
npm install indexed-filter
bower install indexed-filter
Standalone
Download the script file directly.
API
indexedFilter(array, filterFn [, thisObject])
array: Array
filterFn: Function
thisObject: (Any value)
Return: Array
The API is vely similar to ECMAScript's built-in Array#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) 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);
License
Copyright (c) 2015 Shinnosuke Watanabe
Licensed under the MIT License.