Our volunteers haven't translated this article into 正體中文 (繁體) yet. Join us and help get the job done!
This is an experimental technology, part of the ECMAScript 2016 (ES7) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.
The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
Syntax
var boolean = array.includes(searchElement[, fromIndex])
Returns
A Boolean.
Parameters
searchElement- The element to search for.
fromIndex- Optional. The position in this array at which to begin searching for
searchElement. A negative value searches from the end of the string. Defaults to 0.
Examples
[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false [1, 2, 3].includes(3, 3); // false [1, 2, 3].includes(3, -1); // true [1, 2, NaN].includes(NaN); // true
Polyfill
if (!Array.prototype.includes) {
Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
'use strict';
var O = Object(this);
var len = parseInt(O.length) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1]) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {k = 0;}
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) {
return true;
}
k++;
}
return false;
};
}
Specification
Proposal: Array.prototype.includes
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 47 | 43 | Not supported | 34 | 9 |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | Not supported | 47 | 43 | Not supported | 34 | 9 |
See also
Document Tags and Contributors
標籤:
Contributors to this page: jpmedley, tschneidereit, DomenicDenicola, alexlur, fscholz, SphinxKnight, havenchyk, aaylward, Brettz9, ziyunfei, ablaze8, Mingun
最近更新:
jpmedley,