indexOf()

索引辨析

indexOf() 

在字符串中寻找索引

 

1 const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
2 const searchTerm = 'dog';
3 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"

 

findIndex()

var ages = [3, 10, 18, 20];
 
function checkAdult(age) {
    return age >= 18;
}
 
function myFunction() {
    document.getElementById("demo").innerHTML = ages.findIndex(checkAdult);  //2
}

 

寻找的的是数组中元素的索引

posted @ 2020-03-31 15:43  wsh1995  阅读(165)  评论(0编辑  收藏  举报