We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

Il metodo includes() determina se un array include un certo elemento, ritornando truefalse a seconda del caso

Sintassi

var boolean = array.includes(searchElement[, fromIndex])

Parametri

searchElement
L'elemento da cercare.
fromIndex
Opzionale. La posizione nell'array da cui partire per cercare l'elemento searchElement. Un valore negativo ricerca dal valore di array.length + fromIndex in maniera ascendente. Il valore di default è 0.

Valore di ritorno

Un Boolean.

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';
    if (this == null) {
      throw new TypeError('Array.prototype.includes called on null or undefined');
    }

    var O = Object(this);
    var len = parseInt(O.length, 10) || 0;
    if (len === 0) {
      return false;
    }
    var n = parseInt(arguments[1], 10) || 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)) { // NaN !== NaN
        return true;
      }
      k++;
    }
    return false;
  };
}

Specifications

Specification Status Comment
ECMAScript 2016 (ECMA-262)
The definition of 'Array.prototype.includes' in that specification.
Standard Definizione iniziale.
ECMAScript Latest Draft (ECMA-262)
The definition of 'Array.prototype.includes' in that specification.
Draft  

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Edge Opera Safari
Basic support

47

43 No support 14279+ 34 9
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support No support

47

43 No support 34 9

47

Vedi anche

Tag del documento e collaboratori

Hanno collaborato alla realizzazione di questa pagina: mnemosdev, adrisimo74
Ultima modifica di: mnemosdev,