ES7 (一) —— includes

目錄

  • 數組如何判斷元素是否存在?
  • Array.prototype.includes
  • ES6-ES10學習版圖

數組如何判斷元素是否存在?

ES5 filter數組

array1.filter(function (item) { return item === 2 }).length > 0

ES6 用find學習

array1.find(function (item) { return item === 2 })

ES7 includesspa

Array.prototype.includes

Array.prototype.includes()prototype

方法用來判斷一個數組是否包含一個指定的值,根據狀況,若是包含則返回 true,不然返回false。code

const arr = [1,2,3,4,5,7]

console.log(arr.includes(4)) // true
console.log(arr.includes(40)) // false
PS:indexOf沒法查找NaN,使用includes能夠查找NaN

ES6-ES10學習版圖

ES6-10.png

相關文章
相關標籤/搜索