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
1// for dictionary case use findIndex
2var imageList = [
3 {value: 100},
4 {value: 200},
5 {value: 300},
6 {value: 400},
7 {value: 500}
8];
9var index = imageList.findIndex(img => img.value === 200);
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
1var scores = [10, 20, 30, 10, 40, 20];
2console.log(scores.indexOf(30)); // 2
3console.log(scores.indexOf(50)); // -1