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