首页 > 其他 > 详细

[ES2016] Check if an array contains an item using Array.prototype.includes

时间:2017-02-17 19:59:10      阅读:152      评论:0      收藏:0      [点我收藏+]

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 = [1, 2, 3];
a.includes(2); // true 
a.includes(4); // false

 

Search from index:

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

 

NaN problem:

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

 

[ES2016] Check if an array contains an item using Array.prototype.includes

原文:http://www.cnblogs.com/Answer1215/p/6411221.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!