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