From cb9e359a51c3249d8f5157db69d43fd413ddeda6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:45:12 +0100 Subject: unslug ca: move --- .../global_objects/array/findindex/index.html | 173 +++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 files/ca/web/javascript/reference/global_objects/array/findindex/index.html (limited to 'files/ca/web/javascript/reference/global_objects/array/findindex') diff --git a/files/ca/web/javascript/reference/global_objects/array/findindex/index.html b/files/ca/web/javascript/reference/global_objects/array/findindex/index.html new file mode 100644 index 0000000000..5b089bdb98 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/array/findindex/index.html @@ -0,0 +1,173 @@ +--- +title: Array.prototype.findIndex() +slug: Web/JavaScript/Referencia/Objectes_globals/Array/findIndex +translation_of: Web/JavaScript/Reference/Global_Objects/Array/findIndex +--- +
{{JSRef}}
+ +

El mètode findIndex() retorna una posició de l'array si un element de l'array satisfà la funció de testeig donada. En cas contrari retornarà -1.

+ +

Vegeu també el mètode {{jsxref("Array.find", "find()")}}, que retorna el valor trobat dins l'array en comptes de la posició.

+ +

Sintaxi

+ +
arr.findIndex(callback[, thisArg])
+ +

Parameters

+ +
+
callback
+
Funció que s'executarà per a cada valor de l'array, rep tres arguments: +
+
element
+
L'element de l'array que s'està processant actualment.
+
posició
+
La posició de l'array que s'està processant actualment.
+
array
+
L'array des del qual s'ha cridat el mètode find.
+
+
+
thisArg
+
Opcional. L'objecte a utilitzar com a this mentre s'executi callback.
+
+ +

Descripció

+ +

El mètode findIndex executa la funció callback un cop per a cada element present a l'array fins que trobi un on callback retorni true. Si es troba aquest element el mètode findIndex retorna la posició de l'element trobat immediatament. En cas contrari findIndex retornarà -1. callback només serà invocada per a posicions de l'array que tinguin valors assignats; no serà invoada per a posicions que s'hagin eliminat o que mai hagin tingut assignat un valor.

+ +

La invocaicó de callback té tres arguments: el valor de l'element, la posició de l'element i l'objecte array que està sent recorregut.

+ +

Si es proporciona el paràmetre thisArg al cridar el mètode findIndex, aquest serà utilitzat com a this per a cada invocació del mètode callback. En cas de no ser proporcionat s'utilitzarà {{jsxref("undefined")}}.

+ +

findIndex no mutarà l'array des del que es crida.

+ +

El rang d'elemnets que findIndex processarà es determina abans de la primera invocació a callback. Els elements afegits a l'array després de la crida a findIndex no seran visitats per callback. Si un element existent, no visitat encara, rep un altre valor, el valor percebut per callback serà aquell que tingui l'element al ser visitat; els elements visitats no són visitats.

+ +

Exemples

+ +

Trobar la posició d'un nombre primer dins un array

+ +

L'exemple següent trobarà la posició d'un element de l'array que sigui un nombre primer (o bé retornarà -1 si no n'hi ha cap).

+ +
function isPrime(element, index, array) {
+  var start = 2;
+  while (start <= Math.sqrt(element)) {
+    if (element % start++ < 1) {
+      return false;
+    }
+  }
+  return element > 1;
+}
+
+console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, not found
+console.log([4, 6, 7, 12].findIndex(isPrime)); // 2
+
+ +

Polyfill

+ +

Aquest mètode es va afegir a la especificació 6 de l'ECMAScript i pot no estar disponible encara en algunes implementacions de JavaScript. Tot i així es pot utilitzar el codi següent per a utilitzar-lo en entorns on no estigui disponible:

+ +
if (!Array.prototype.findIndex) {
+  Array.prototype.findIndex = function(predicate) {
+    if (this === null) {
+      throw new TypeError('Array.prototype.findIndex called on null or undefined');
+    }
+    if (typeof predicate !== 'function') {
+      throw new TypeError('predicate must be a function');
+    }
+    var list = Object(this);
+    var length = list.length >>> 0;
+    var thisArg = arguments[1];
+    var value;
+
+    for (var i = 0; i < length; i++) {
+      value = list[i];
+      if (predicate.call(thisArg, value, i, list)) {
+        return i;
+      }
+    }
+    return -1;
+  };
+}
+
+ +

Especificacions

+ + + + + + + + + + + + + + + + + + + +
EspecificacióEstatComentaris
{{SpecName('ES6', '#sec-array.prototype.findIndex', 'Array.prototype.findIndex')}}{{Spec2('ES6')}}Definició inicial.
{{SpecName('ESDraft', '#sec-array.prototype.findIndex', 'Array.prototype.findIndex')}}{{Spec2('ESDraft')}} 
+ +

Compatibilitat amb navegadors

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
CaracterísticaChromeFirefox (Gecko)Internet ExplorerOperaSafari
Suport bàsic{{CompatChrome(45.0)}}{{CompatGeckoDesktop("25.0")}}{{CompatNo}}{{CompatNo}}{{CompatSafari("7.1")}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
CaracterísticaAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Suport bàsic{{CompatNo}}{{CompatNo}}{{CompatGeckoMobile("25.0")}}{{CompatNo}}{{CompatNo}}8.0
+
+ +

Vegeu també

+ + -- cgit v1.2.3-54-g00ecf