indexOf()
1 var num = [1, 7, 2, 3, 4, 7, 9]
2 var pos = num.indexOf(7)
3 var pos1 = num.indexOf(7, 2)
4 console.log(pos); // => 1
5 console.log(pos1); // => 5
6
7 //封装一个方法实现indexOf的功能
8 function ArrayIndexOF(arr, value) {
9 //检测value在arr中出现的位置
10 for(var i = 0; i < arr.length; i++) {
11 if(arr[i] === value) {
12 return i;
13 }
14 }
15 return -1;
16 }
17
18 console.log(ArrayIndexOF(num, 7)); // => 1
lastIndexOf()
1 var num = [1, 7, 2, 3, 4, 7, 9]
2 var pos = num.lastIndexOf(7)
3 var pos1 = num.lastIndexOf(7,2)
4 console.log(pos); // => 5
5 console.log(pos1); // => 1