@writetome51/array-get-by-test

Similar to Array.prototype.filter(), except it lets you customize what value to get from the element that passes test

Usage no npm install needed!

<script type="module">
  import writetome51ArrayGetByTest from 'https://cdn.skypack.dev/@writetome51/array-get-by-test';
</script>

README

getByTest(
      test: (value, index?, array?) => boolean,
      array,
      getValue?: (value, index?, array?) => any
): any[]

Similar to Array.prototype.filter(), except it includes optional callback getValue(),
which lets you customize what value to get from the element that passes test.
By default getValue() simply returns the element.

Examples

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
getByTest((value) => value >= 9, arr);
// --> [9, 10]


// Return the value and index of each element that passes:

arr = [50, -10, 100, -20, 1000, -100];
getByTest(
    (value) => value < 0, 
    arr,
    (value, index) => { return {value, index}; }
);
/*************** 
Returns:
[
   {value: -10, index: 1}, 
   {value: -20, index: 3}, 
   {value: -100, index: 5}
]
***************/


// Get only the indexes:

arr = [50, -10, 100, -20, 1000, -100];
getByTest(
    (value) => value < 0, 
    arr, 
    (value, index) => index
);
// --> [1, 3, 5]


// Get the square root of each element that passes:

arr = [-2,-4, 9, 16];
getByTest(
    (value) => value > 0,
    arr,
    (value) => Math.sqrt(value)
);
// --> [3, 4]

Installation

npm i @writetome51/array-get-by-test

Loading

import {getByTest} from '@writetome51/array-get-by-test';