1.比較傳統的做法:
概述: indexOf()方法返回給定元素能找在數組中找到的第一個索引值,否則返回-1。
var a=["1", "3", "2", "3", "0"]; console.log(a.indexOf ('3'));//1
console.log(a.indexOf (5,'0'));//-1
console.log(a.indexOf ('0'));//4
console.log(a.indexOf ('0',-5));//4
console.log(a.indexOf ('3',-1));//-1
console.log(a.indexOf ('3',-3));//3
console.log(a.indexOf ('1',5));//-1
console.log(a.indexOf (3));//-1
console.log(a);//["1", "3", "2", "3", "0"]
2.今天發現有個函數比較好用
let arr = [1,2,3]; arr.includes(1) // true
arr.includes(5) //false
關於數組的更多解讀和函數:參考es6學習筆記一數組(下)
