js array find index of element code example
Example 1: js find index in list
let myList = ['foo', 'bar', 'qux'];
myList.indexOf('bar'); // 1
Example 2: findindex js
// findIndex(callback fn)
// .... return index (when condition meets)
// .... return -1 (if condition not meets)
const array = [5, 12, 8, 130, 44];
/// it returns the index of number which satisfy the condition true
const index = array.findIndex((item)=> item>10); //1
/// now we can check what element at that index...
console.log(array[index]); // array[1]
Example 3: index of value in array
// for dictionary case use findIndex
var imageList = [
{value: 100},
{value: 200},
{value: 300},
{value: 400},
{value: 500}
];
var index = imageList.findIndex(img => img.value === 200);
Example 4: js array get index
var fruits = ["Banana", "Orange", "Apple", "Mango"];
return fruits.indexOf("Apple"); // Returns 2
Example 5: get index of element in array javascript
var scores = [10, 20, 30, 10, 40, 20];
console.log(scores.indexOf(30)); // 2
console.log(scores.indexOf(50)); // -1