We often want to check if an array includes a specific item. It's been common to do this with the Array.prototype.indexOf method, but now we have a simpler way: We can use the Array.prototype.includes method, which is available starting with ES2016.

Normal case:

var a = [, , ];
a.includes(); // true
a.includes(); // false

Search from index:

[, , ].includes(, );  // false
[, , ].includes(, -); // true

NaN problem:

[, , NaN].includes(NaN); // true
[, , NaN].indexOf(NaN); // -1

[ES2016] Check if an array contains an item using Array.prototype.includes的更多相关文章

  1. js & array remove one item ways

    js & array remove one item ways // array remove one item ways let keys = [1,2,3,4,5,6,7]; let ke ...

  2. Why is processing a sorted array faster than an unsorted array?

    这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...

  3. ES7学习笔记——Array.prototype.includes和求幂运算符**

    一直以来,在前端开发时使用的基本都是ES5,以及少量的ES6.3月份换工作面试时,发现一些比较大的公司,对ES6比较重视,阿里的面试官直接问ES7和ES8,对于从未接触过人来说,完全是灾难.由此也显现 ...

  4. Array.prototype.includes

    if (!Array.prototype.includes) {   Array.prototype.includes = function(searchElement /*, fromIndex*/ ...

  5. 数组Array和字符串String的indexOf方法,以及ES7(ES2016)中新增的Array.prototype.includes方法

    前言 我们在判断某一个字符是否存在于一个字符串中或者某一个值是否存在于一个数组中时,ES7之前我们需要使用indexOf,ES7引入了新的方法includes 语法 数组:Array.inexOf(s ...

  6. JavaScript json loop item in array

    Iterating through/Parsing JSON Object via JavaScript 解答1 Your JSON object is incorrect because it ha ...

  7. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  8. 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  9. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

随机推荐

  1. 1.2 Use Cases中 Messaging官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 下面是一些关于Apache kafka 流行的使用场景.这些领域的概述,可查看博客文 ...

  2. 报错 关于python的x和y不等长

    ValueError: shape mismatch: objects cannot be broadcast to a single shape 这个错误可能是因为传入的两个参数数据长度不一样,比如 ...

  3. python之经典猜数字

    题目:猜数字1.让用户输入1-20,猜数字,可以猜5次.2.每次有提示,大了,或者小了!3.如果超过5次,提示game over. # !/usr/bin/env python # -*- codin ...

  4. 浅谈Normalize.css

    浅谈Normalize.css 一.总结 1.Normalize.css:它在默认的HTML元素样式上提供了跨浏览器的高度一致性,花了几百个小时来努力研究不同浏览器的默认样式的差异. 2.优于rese ...

  5. linux开发板的启动

    转载:http://blog.csdn.net/mr_raptor/article/details/6555667 虽然有很多地方并不是很明白,但是可以先记下 嵌入式系统启动过程 转载 2014年09 ...

  6. 使用mybatis generator 生成annotation形式的mapper

    最近使用mybatisgenerator时,希望生成annotation的mapper,在网上基本桑没有相关配置,记录,做以下记录: <?xml version="1.0"  ...

  7. vc6.0 点编译时提示Cannot complile the file &#39;D:\souce-code\vc-workspace\对话框\MainFrm.h&#39;; no compile tool is

    问题描写叙述: vc6.0程序,点击编译时提示对话框,内容为: Cannot complile the file 'D:\souce-code\vc-workspace\对话框\MainFrm.h'; ...

  8. numpy 细节问题

    1. np.expand_dims >> X = np.random.randint(0, 9, (2, 3)) >> mean_X = np.mean(X, axis=0) ...

  9. Intel X86 CPU 系列的寻址方式

    Intel X86 CPU 系列的寻址方式 数据总线和地址总线要尽量相同,这个是一个地址就是一个指针.

  10. SpringBoot日志logback-spring.xml分环境(转)

    springboot按照profile进行打印日志 log4j logback slf4j区别? 首先谈到日志,我们可能听过log4j logback slf4j这三个名词,那么它们之间的关系是怎么样 ...