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
1//indexOf - JS method to get index of array element.
2// Returns -1 if not found
3
4var colors=["red","green","blue"];
5var pos=colors.indexOf("blue");//2
6
7//indexOf getting index of sub string, returns -1 if not found
8
9var str = "We got a poop cleanup on isle 4.";
10var strPos = str.indexOf("poop");//9
11
12//Eg with material ui
13
14<Checkbox
15 checked={value.indexOf(option) > -1}
16 value={option}
17/>
1var indices = [];
2var array = ['a', 'b', 'a', 'c', 'a', 'd'];
3var element = 'a';
4var idx = array.indexOf(element);
5while (idx != -1) {
6 indices.push(idx);
7 idx = array.indexOf(element, idx + 1);
8}
9console.log(indices);
10// [0, 2, 4]
11
1//indexOf getting index of array element, returns -1 if not found
2var colors=["red","green","blue"];
3var pos=colors.indexOf("blue");//2
4
5//indexOf getting index of sub string, returns -1 if not found
6var str = "We got a poop cleanup on isle 4.";
7var strPos = str.indexOf("poop");//9
1
2var str = "Please locate where 'locate' occurs!";
3
4var ind1 = str.indexOf("locate"); // return location of first value which founded
5var ind2 = str.lastIndexOf("locate"); // return location of last value which founded
6var ind3 = str.indexOf("locate", 15); // start search from location 15 and then take first value which founded
7var ind4 = str.search("locate");
8//The search() method cannot take a second start position argument.
9//The indexOf() method cannot take powerful search values (regular expressions).
10
11document.write("<br>" + "Length of string:", len);
12document.write("<br>" + "indexOf:", ind1);
13document.write("<br>" + "index of last:", ind2);
14document.write("<br>" + "indexOf with start point:", ind3);
15document.write("<br>" + "search:", ind4);