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
原文地址:https://www.cnblogs.com/hlhs/p/12508340.html
时间: 2024-10-07 04:15:13