Underscore.js | _.keepIndexed() Method
The Underscore.js _.keepIndexed() Method takes an array and a function as parameters and returns a new array filled with the non-null return results of the given function acted upon the elements of the given array.
- array: The array passed to be passed to this method.
- function: The function containing the conditions to generate a new array.
Return Value: This method returns a newly generated array.
underscore.js contrib library can be installed using npm install underscore-contrib –save.
Example 1: In this example, we will generate an array using this method by checking conditions.
Here, in the function, the index of the array is passed which is further used to get values and comparison.
Generated Array : [ false, false, true, true ]
Example 2: In this example, we will generate an array full of indexes of elements.
Generated Array : [ 0, 1, 2, 3, 4, 5, 6, 7 ]
Example 3: In this example, we will use if condition to get particular values.
Generated Array : [ 11 ]