naykhit
0
Q:

indexof

//indexOf getting index of array element, returns -1 if not found
var colors=["red","green","blue"];
var pos=colors.indexOf("blue");//2

//indexOf getting index of sub string, returns -1 if not found
var str = "We got a poop cleanup on isle 4.";
var strPos = str.indexOf("poop");//9
30
//indexOf - JS method to get index of array element. 
// Returns -1 if not found

var colors=["red","green","blue"];
var pos=colors.indexOf("blue");//2

//indexOf getting index of sub string, returns -1 if not found

var str = "We got a poop cleanup on isle 4.";
var strPos = str.indexOf("poop");//9

//Eg with material ui

<Checkbox
   checked={value.indexOf(option) > -1}
   value={option}
/>
14
var indices = [];
var array = ['a', 'b', 'a', 'c', 'a', 'd'];
var element = 'a';
var idx = array.indexOf(element);
while (idx != -1) {
  indices.push(idx);
  idx = array.indexOf(element, idx + 1);
}
console.log(indices);
// [0, 2, 4]
1
/"returns the index, of that element, or -1 if the element does not exist."/

let fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears'];

fruits.indexOf('dates'); // returns -1
fruits.indexOf('oranges'); // returns 2
fruits.indexOf('pears'); // returns 1, first index at which the element exists
4
var array = [2, 9, 9];
array.indexOf(2);     // 0
array.indexOf(7);     // -1
array.indexOf(9, 2);  // 2
array.indexOf(2, -1); // -1
array.indexOf(2, -3); // 0
1

var str = "Hello world, welcome to the universe.";

var n = str.indexOf("welcome");
 
0
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);

console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
// expected output: "The index of the first "dog" from the beginning is 40"

console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// expected output: "The index of the 2nd "dog" is 52"
1
let monTableau = ['un', 'deux','trois', 'quatre'] ;
console.log(monTableau.indexOf('trois')) ;
 
2
"Blue Whale".indexOf("Blue");     // retorna  0
"Blue Whale".indexOf("Blute");    // retorna -1
"Blue Whale".indexOf("Whale", 0); // retorna  5
"Blue Whale".indexOf("Whale", 5); // retorna  5
"Blue Whale".indexOf("Whale", 7); // retorna -1
"Blue Whale".indexOf("");         // retorna  0
"Blue Whale".indexOf("", 9);      // retorna  9
"Blue Whale".indexOf("", 10);     // retorna 10
"Blue Whale".indexOf("", 11);     // retorna 10
1
str.indexOf(searchValue[, fromIndex])
0
String1.indexOf(index2); 
0

New to Communities?

Join the community