find index array js code example

Example 1: how to find the index of a value in an array in javascript

var list = ["apple","banana","orange"]

var index_of_apple = list.indexOf("apple") // 0

Example 2: javascript findindex

const array1 = [5, 12, 8, 130, 44];
const search = element => element > 13;
console.log(array1.findIndex(search));
// expected output: 3

const array2 = [
  { id: 1, dev: false },
  { id: 2, dev: false },
  { id: 3, dev: true }
];
const search = obj => obj.dev === true;
console.log(array2.findIndex(search));
// expected output: 2

Example 3: js find index in list

let myList = ['foo', 'bar', 'qux'];

myList.indexOf('bar');	// 1

Example 4: get array by array of indices js

var resultArr = indexArr.map(i => fruitier[i])

Example 5: access index of array javascript

let first = fruits[0]
// Apple

let last = fruits[fruits.length - 1]
// Banana