1arr = [{x: "a", y: 1}, {x: "something", y: 2}]
2arr.findIndex(obj => obj.x === "a" && obj.y === 1);
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);
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 hello = {
2 hello: 'world',
3 foo: 'bar'
4};
5var qaz = {
6 hello: 'stevie',
7 foo: 'baz'
8}
9
10var myArray = [];
11myArray.push(hello,qaz);
12
13var indexOfStevie = myArray.findIndex(i => i.hello === "stevie");
14