1// Get index of object with specific value in array
2const needle = 3; // needle
3const haystack = [{ id: 1 }, { id: 2 }, { id: 3 }]; // haystack
4const index = haystack.findIndex(item => item.id === needle);
1// Get index of object with specific value in array
2const needle = 3;
3const haystack = [{ id: 1 }, { id: 2 }, { id: 3 }];
4const index = haystack.findIndex(item => item.id === needle);
1var list = ["apple","banana","orange"]
2
3var index_of_apple = list.indexOf("apple") // 0
1const array1 = [5, 12, 8, 130, 44];
2const search = element => element > 13;
3console.log(array1.findIndex(search));
4// expected output: 3
5
6const array2 = [
7 { id: 1, dev: false },
8 { id: 2, dev: false },
9 { id: 3, dev: true }
10];
11const search = obj => obj.dev === true;
12console.log(array2.findIndex(search));
13// expected output: 2
1const letters = [{letter: 'a'},{letter: 'b'},{letter: 'c'}]
2
3const index = letters.findIndex((element, index) => {
4 if (element.letter === 'b') {
5 return true
6 }
7})
8
9//index is `1`
1const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
2
3beasts.indexOf('bison'); //ouput: 1
4
5// start from index 2
6beasts.indexOf('bison', 2); //output: 4
7
8beasts.indexOf('giraffe'); //output: -1