array-remove-if
v1.0.0
Published
Remove all array elements that satisfy the given condition.
Downloads
3
Readme
array-remove-if
Remove all array elements that satisfy the given condition.
Installation
Add dependency:
npm install array-remove-if
Then require the module:
require('array-remove-if');
This module adds the removeIf method to Array's prototype, and does not export anything.
Usage
Syntax:
var removedElements = array.removeIf(callback);
Parameters:
Return value: A new array with the removed elements.
Examples
Remove even numbers:
var x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var y = x.removeIf(e=>e % 2 == 0);
// x = [1, 3, 5, 7, 9]
// y = [2, 4, 6, 8, 10]
Remove words containing 'r':
var x = ['collect', 'enormous', 'drawer', 'art', 'thought', 'burly', 'special', 'claim', 'troubled', 'aboard'];
var y = x.removeIf(e=>e.indexOf('r') > -1);
// x = ['collect', 'thought', 'special', 'claim']
// y = ['enormous', 'drawer', 'art', 'burly', 'troubled', 'aboard']
Remove all elements which are not a number:
var x = [0, 1, 'string'];
var y = x.removeIf(e=>typeof e !== 'number');
// x = [0, 1]
// y = ['string']
License
MIT © Arthur Gay