A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-findindex-method/ below:

Lodash _.findIndex() Method - GeeksforGeeks

Lodash _.findIndex() Method

Last Updated : 02 Sep, 2024

Loadsh _.findIndex() method is used to find the index of the first occurrence of the element. It is different from indexOf because it takes the predicate function that iterates through each element of the array.

Syntax:
_.findIndex(array, [predicate=_.identity], fromIndex);
Parameters: Return Value:

Example 1: In this example, an element is searched starting from index 0 and printing the result according to the arrow function used in the _.findIndex() method.

JavaScript
// Requiring the lodash library
const _ = require('lodash');

// Original array
let array1 = [4, 2, 3, 1, 4, 2]

// Using lodash.findIndex
let index = _.findIndex(array1, (e) => {
    return e == 1;
}, 0);

// Print original Array
console.log("original Array: ", array1)

// Printing the index
console.log("index: ", index)

Output:

Example 2: In this example, an element is looked after some index "i". Here element is present in the array but still the output is -1 because it is present at index 3 and the searching starts from index 5.

JavaScript
// Requiring the lodash library
const _ = require('lodash');

// Original array
let array1 = [4, 2, 3, 1, 4, 2]

// Using lodash.findIndex
let index = _.findIndex(array1, (e) => {
    return e == 1;
}, 5);

// Print original Array
console.log("original Array: ", array1)

// Printing the index
console.log("index: ", index)

Output:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4