Wednesday, July 3, 2024
HomeLanguagesJavascriptUnderscore.js | _.keepIndexed() Method

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 acting upon the elements of the given array.

Syntax:

_.keepIndexed(array, function)

Parameters:

  • 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.

Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.

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.

javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
// Defining Array
let array = [1, 3, 5, 9]
// Using keepIndexed() Method
arr = _.keepIndexed(array, function (n) {
    return array[n] >= 5;
});
 
console.log("Generated Array : ");
console.log(arr);


Output:

Generated Array :
[ false, false, true, true ]

Example 2: In this example, we will generate an array full of indexes of elements.

javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
// Defining Array
let array = [1, 3, 5, 9, 11, 22, 34, 55]
// Using keepIndexed() Method
arr = _.keepIndexed(array, function (n) {
    return n;
});
 
console.log("Generated Array : ");
console.log(arr);


Output:

Generated Array :
[
  0, 1, 2, 3,
  4, 5, 6, 7
]

Example 3: In this example, we will use the if condition to get particular values.

javascript




// Defining underscore contrib variable
const _ = require('underscore-contrib');
// Defining Array
let array = [1, 3, 5, 9, 11, 22, 34, 55]
// Using keepIndexed() Method
arr = _.keepIndexed(array, function (n) {
    if (n === 4) return array[n];
});
 
console.log("Generated Array : ");
console.log(arr);


Output:

Generated Array :
[ 11 ]

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments