From 869dd2069c695ee7040cd3261713212155819f42 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Mon, 14 Dec 2020 12:18:12 -0500 Subject: final dump 2020-12-14 --- .../objetos_globales/array/filter/index.html | 56 +++++++++++++--------- .../referencia/objetos_globales/array/index.html | 8 ++-- 2 files changed, 37 insertions(+), 27 deletions(-) (limited to 'files/es/web/javascript') diff --git a/files/es/web/javascript/referencia/objetos_globales/array/filter/index.html b/files/es/web/javascript/referencia/objetos_globales/array/filter/index.html index dae88f3ece..98e6843c4f 100644 --- a/files/es/web/javascript/referencia/objetos_globales/array/filter/index.html +++ b/files/es/web/javascript/referencia/objetos_globales/array/filter/index.html @@ -19,7 +19,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/filter

Sintaxis

-
var newArray = arr.filter(callback(currentValue[, index[, array]])[, thisArg])
+
var newArray = arr.filter(callback(currentValue[, index[, array]])[, thisArg])

Parámetros

@@ -64,11 +64,11 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/filter

Ejemplos

-

Filtrando todos los valores pequeños

+

Filtrando todos los valores pequeños

El siguiente ejemplo usa filter() para crear un array filtrado que excluye todos los elementos con valores inferiores a 10.

-
function esSuficientementeGrande(elemento) {
+
function esSuficientementeGrande(elemento) {
   return elemento >= 10;
 }
 var filtrados = [12, 5, 8, 130, 44].filter(esSuficientementeGrande);
@@ -79,7 +79,7 @@ var filtrados = [12, 5, 8, 130, 44].filter(esSuficientementeGrande);
 
 

El siguiente ejemplo emplea filter() para crear un json filtrado con todos lo elementos que tengan id numérico distinto de cero.

-
var arr = [
+
var arr = [
   { id: 15 },
   { id: -1 },
   { id: 0 },
@@ -115,7 +115,7 @@ console.log('Número de Entradas Invalidas = ', entradasInvalidas);
 
 

El siguiente ejemplo emplea filter() para filtrar el contendio de un arreglo en función de un criterio de búsqueda.

-
var fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
+
var fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
 
 /**
  * Filtra la matríz en función de un criterio de búsqueda (query)
@@ -131,7 +131,7 @@ console.log(filterItems('an')); // ['banana', 'mango', 'orange']

Implementación en ES2015

-
const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
+
const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
 
 /**
  * Filtra la matríz en función de un criterio de búsqueda (query)
@@ -149,32 +149,44 @@ console.log(filterItems('an')); // ['banana', 'mango', 'orange']

filter() se añadió a la norma ECMA-262 en la 5ta edición; como tal puede no estar presente en todas las implementaciones de la norma. Puedes sobrellevarlo insertando el siguiente código al comienzo de su programa, para permitir el uso de filter() en implementaciones de ECMA-262 que no lo soporten de forma nativa. Este algoritmo es exactamente el especificado en ECMA-262, 5ta edición, supone que  fn.call evalua al valor original de {{jsxref("Function.prototype.call")}}, y que {{jsxref("Array.prototype.push")}} tiene su valor original.

-
if (!Array.prototype.filter)
+
if (!Array.prototype.filter){
   Array.prototype.filter = function(func, thisArg) {
     'use strict';
-    if ( ! ((typeof func === 'Function') && this) )
+    if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
         throw new TypeError();
 
     var len = this.length >>> 0,
         res = new Array(len), // preallocate array
-        c = 0, i = -1;
-    if (thisArg === undefined)
-      while (++i !== len)
+        t = this, c = 0, i = -1;
+
+    var kValue;
+    if (thisArg === undefined){
+      while (++i !== len){
         // checks to see if the key was set
-        if (i in this)
-          if (func(t[i], i, t))
-            res[c++] = t[i];
-    else
-      while (++i !== len)
+        if (i in this){
+          kValue = t[i]; // in case t is changed in callback
+          if (func(t[i], i, t)){
+            res[c++] = kValue;
+          }
+        }
+      }
+    }
+    else{
+      while (++i !== len){
         // checks to see if the key was set
-        if (i in this)
-          if (func.call(thisArg, t[i], i, t))
-            res[c++] = t[i];
+        if (i in this){
+          kValue = t[i];
+          if (func.call(thisArg, t[i], i, t)){
+            res[c++] = kValue;
+          }
+        }
+      }
+    }
 
     res.length = c; // shrink down array to proper size
     return res;
   };
-
+}

Especificaciones

@@ -193,12 +205,12 @@ console.log(filterItems('an')); // ['banana', 'mango', 'orange']
{{SpecName('ES6', '#sec-array.prototype.filter', 'Array.prototype.filter')}} {{Spec2('ES6')}} -   + {{SpecName('ESDraft', '#sec-array.prototype.filter', 'Array.prototype.filter')}} {{Spec2('ESDraft')}} -   + diff --git a/files/es/web/javascript/referencia/objetos_globales/array/index.html b/files/es/web/javascript/referencia/objetos_globales/array/index.html index e4358cf6d1..45531c7a3e 100644 --- a/files/es/web/javascript/referencia/objetos_globales/array/index.html +++ b/files/es/web/javascript/referencia/objetos_globales/array/index.html @@ -358,7 +358,7 @@ if (mensajes.length === 100) { [' ',' ',' ',' ',' ',' ',' ',' '], [' ',' ',' ',' ',' ',' ',' ',' '], ['p','p','p','p','p','p','p','p'], - ['r','c','a','d','r','a','c','t'] ] + ['t','c','a','d','r','a','c','t'] ] console.log(tablero.join('\n') + '\n\n') @@ -376,7 +376,7 @@ P,P,P,P,P,P,P,P  , , , , , , ,  , , , , , , , p,p,p,p,p,p,p,p -r,c,a,d,r,a,c,t +t,c,a,d,r,a,c,t P,P,P,P,P,P,P,P  , , , , , , , @@ -384,11 +384,9 @@ P,P,P,P,P,P,P,P  , , , ,p, , ,  , , , , , , , p,p,p,p, ,p,p,p -r,c,a,d,r,a,c,t +t,c,a,d,r,a,c,t
- -

Uso de un array para tabular un conjunto de valores

valores = []
-- 
cgit v1.2.3-54-g00ecf