Wednesday, July 3, 2024
HomeLanguagesJavascriptLodash _.keepindexed() Method

Lodash _.keepindexed() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.

The _.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.

Syntax:

_.keepIndexed( array, function )

Parameters: This method accepts two parameters as mentioned above and described below:

  • array: This is the array to be passed to this method.
  • function: This is 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 lodash-contrib library to be installed. The lodash-contrib library can be installed using npm install lodash-contrib –save.

Example 1: In this example, we will generate an array using this method by checking conditions. The index of the array is passed in the function which is further used to get values and comparison.

Javascript




// Defining Lodash-contrib variable 
const _ = require('lodash-contrib'); 
  
// Defining Array
var array = [1, 3, 5, 9]
  
// Using the _.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 Lodash-contrib variable 
const _ = require('lodash-contrib'); 
  
// Defining Array
var 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 Lodash-contrib variable 
const _ = require('lodash-contrib'); 
  
// Defining Array
var array = [1, 3, 5, 9, 11, 22, 34, 55]
  
// Using _.keepIndexed() Method
arr = _.keepIndexed(array, function(n) { 
  if(n===5) return array[n];
});
  
console.log("Generated Array : ");
console.log(arr);


Output:

Generated Array :
[ 22 ]

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!

Dominic Rubhabha Wardslaus
Dominic Rubhabha Wardslaushttps://neveropen.dev
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments