How to useLodash _.indexOf() Method in Javascript
lodash _.indexOf() method is used to get the index of the first occurrence of the particular element in the array. If fromIndex is not present in the array negative one is given as the output and no error is displayed.
Syntax:
indexOf(array, value, fromIndex);
Example: In this example, we are getting the index at which the given value is present in the given array.
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array = [1, 2, 3, 4]
// Looking for value 3 from index 0
let index = _.indexOf(array, 3, 0)
// Printing the Index of the value
console.log("Index : ", index)
Output:
2
How to Find the Index of Specific Character in a String in JavaScript ?
We will see how to find the index of a specific character in a string using JavaScript. A string is a sequence of characters, such as letters, numbers, or symbols, enclosed in single or double quotes. Here we are going to find the index of a specific character from our given string.
Several approaches can be used to find the index of a specific character in a string using JavaScript, which are listed below:
Table of Content
- Approach 1: Using String indexOf() Method
- Approach 2: Using String search() Method
- Approach 3: Using String match() Method
- Approach 4: Using String lastIndexOf() Method
- Approach 5: Using Lodash _.indexOf() Method
- Approach 6: Using a for loop
- Approach 7: Using ES6 findIndex() Method with Array.from()
Contact Us