1var list = ["apple","banana","orange"]
2
3var index_of_apple = list.indexOf("apple") // 0
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/>
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);
1a = [
2 {prop1:"abc",prop2:"qwe"},
3 {prop1:"bnmb",prop2:"yutu"},
4 {prop1:"zxvz",prop2:"qwrq"}
5];
6
7index = a.findIndex(x => x.prop2 ==="yutu");
8
9console.log(index);
1const fruits = ["apple", "banana", "cantaloupe", "blueberries", "grapefruit"];
2
3const index = fruits.findIndex(fruit => fruit === "blueberries");
4
5console.log(index); // 3
6console.log(fruits[index]); // blueberries
7