ECMAScript 2016 incorporates an includes()
method for arrays that specifically solves the problem, and so is now the preferred method.
(ECMAScript 2016为特定解决问题的数组采用了includes()
方法,因此现在是首选方法。)
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(4); // false
[1, 2, 3].includes(1, 2); // false (second parameter is the index position in this array at which to begin searching)
As of JULY 2018, this has been implemented in almost all major browsers, if you need to support IE a polyfill is available.
(截至2018年7月,几乎所有主流浏览器都已实现 ,如果您需要支持IE,则可以使用polyfill 。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…