https://github.com/ashubham/array-filter-n
Filter the first n elements of an array, Fast!
https://github.com/ashubham/array-filter-n
Last synced: 22 days ago
JSON representation
Filter the first n elements of an array, Fast!
- Host: GitHub
- URL: https://github.com/ashubham/array-filter-n
- Owner: ashubham
- Created: 2015-10-01T05:27:30.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-11-05T03:19:00.000Z (over 9 years ago)
- Last Synced: 2025-03-02T21:38:02.500Z (about 2 months ago)
- Language: JavaScript
- Homepage:
- Size: 184 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# array-filter-n
The filterN() method creates a new array with the first N elements that pass the test implemented by the provided function. Fast!
## Syntax
`filterN(inputArr, N, checkFn[, thisArg])`
The checkFn is the standard EcmaScript `function (elem, index, array)`
`thisArg` if supplied is set as the `this` context for `checkFn`.
## Usage
```javascript
var filterN = require('array-filter-n');var filter4even =
filterN([12, 8, 3, 1, 0, 4, 7, 9, 12], 4, function(i) { return !i%2});
//=> [12, 8, 0, 4]// works also on strings
var filter3nums =
filterN('n0d3_R0ck5', 3, function(c) { return !isNaN(parseInt(c)); });
//=> [0, 3, 0]// Fastened safety belts
var first4pallindromes =
filterN(['aat', 'aba', 'pop'], 5, function (s) {
s == s.split('').reverse().join('');
});
//=> ['aba', 'pop']var crazy = filterN([1,2,3,4], -3);
//=> []var notSoCrazy = filterN([1,2,3,4], 3);
//=> [1, 2, 3]
```