Loading... **方法一: arr.length** > ``` > > let arr = []; > > if (arr.length == 0){ > > console.log("数组为空") > > }else { > > console.log("数组不为空") > > } > ``` **方法二、js判断数组是否含有某个值** > ``` > arr.indexOf() > > if (arr.indexOf(2) != -1){ > console.log("数组含有2") > }else { > console.log("数组不含2") > } > ``` **方法三: for循环结合if判断** > ``` > for (let i = 0;i < arr.length;i++){ > if (arr[i] === 2){ > console.log("数组含有2") > } > } > ``` **方法四: arr.find(callback)** > ``` > arr.find(value => { > if (value === 2){ > console.log("数组含有2") > } > }) > ``` **方法五: arr.includes()** 数组中含有某值返回true,没有返回false。ES7新方法。 > ``` > let arr = [1,2,3,4]; > if(arr.includes(2)){ > console.log("数组中有2"); > }else{ > console.log("数组中无2"); > } > ``` Last modification:August 16, 2022 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 0 如果觉得我的文章对你有用,请随意赞赏
One comment
很有用