| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Array/some | [Back] [Original] |
Get to know MDN better
Esta pgina ha sido traducida del ingls por la comunidad. Aprende ms y nete a la comunidad de MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since julio de 2015.
El mtodo some() comprueba si al menos un elemento del array cumple con la condicin implementada por la funcin proporcionada.
Nota:
Este mtodo devuelve false para cualquier condicin puesta en un array vaco.
const array = [1, 2, 3, 4, 5];
// Checks whether an element is even
const even = (element) => element % 2 === 0;
console.log(array.some(even));
// Expected output: true
arr.some(callback(element[, index[, array]])[, thisArg])
callbackFuncin que verifica cada elemento, toma tres argumentos:_ element
_ : El elemento actual siendo procesado en el array.
index OpcionalEl ndice del elemento del array que se est procesando.
array OpcionalEl array sobre el que ha sido llamada la funcin some().
thisArg OpcionalValor a usar como this cuando se ejecute callback.
true si la funcin callback devuelve un valor truthy para cualquier elemento del array; en caso contrario, false.
some() ejecuta la funcin callback una vez por cada elemento presente en el array hasta que encuentre uno donde callback retorna un valor verdadero (true). Si se encuentra dicho elemento, some() retorna true inmediatamente. Si no, some() retorna false. callback es invocada slo para los ndices del array que tienen valores asignados; no es invocada para ndices que han sido borrados o a los que nunca se les han asignado valores.
callback es invocada con tres argumentos: el valor del elemento, el ndice del elemento, y el objeto array sobre el que se itera.
Si se indica un parmetro thisArg a some(), se pasar a callback cuando es invocada, para usar como valor this. Si no, el valor undefined ser pasado para usar como valor this. El valor this value observable por callback se determina de acuerdo a las reglas habituales para determinar el this visible por una funcin.
some() no modifica el array con el cual fue llamada.
El rango de elementos procesados por some() es configurado antes de la primera invocacin de callback. Los elementos anexados al array luego de que comience la llamada a some() no sern visitados por callback. Si un elemento existente y no visitado del array es alterado por callback, su valor pasado al callback ser el valor al momento que some() visita el ndice del elemento; los elementos borrados no son visitados.
El siguiente ejemplo verifica si algn elemento del array es mayor a 10.
function isBiggerThan10(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
Las funciones flecha (Arrow functions) brindan una sintxis ms corta para el mismo test.
[2, 5, 8, 1, 4].some((elem) => elem > 10); // false
[12, 5, 8, 1, 4].some((elem) => elem > 10); // true
Para imitar la funcin del mtodo includes(), esta funcin personalizada devuelve true si el elemento existe en el array:
var fruits = ["apple", "banana", "mango", "guava"];
function checkAvailability(arr, val) {
return arr.some(function (arrVal) {
return val === arrVal;
});
}
checkAvailability(fruits, "kela"); // false
checkAvailability(fruits, "banana"); // true
var fruits = ["apple", "banana", "mango", "guava"];
function checkAvailability(arr, val) {
return arr.some((arrVal) => val === arrVal);
}
checkAvailability(fruits, "kela"); // false
checkAvailability(fruits, "banana"); // true
var TRUTHY_VALUES = [true, "true", 1];
function getBoolean(value) {
"use strict";
if (typeof value === "string") {
value = value.toLowerCase().trim();
}
return TRUTHY_VALUES.some(function (t) {
return t === value;
});
}
getBoolean(false); // false
getBoolean("false"); // false
getBoolean(1); // true
getBoolean("true"); // true
some() fue agregado al estndar ECMA-262 en la 5ta edicin; por ello, puede no estar presente en todas las implementaciones del estndar. Puedes trabajar sobre esto insertando el siguiente cdigo al comienzo de tus scripts, permitiendo el uso de some() en implementaciones que no tienen soporte nativo. Este algoritmo es exactamente el mismo especificado en ECMA-262, 5ta edicin, asumiendo que Object y TypeError tienen sus valores originales y que fun.call evala el valor original deFunction.prototype.call().
// Pasos de produccin de ECMA-262, Edicin 5, 15.4.4.17
// Referencia: http://es5.github.io/#x15.4.4.17
if (!Array.prototype.some) {
Array.prototype.some = function (fun /*, thisArg*/) {
"use strict";
if (this == null) {
throw new TypeError("Array.prototype.some called on null or undefined");
}
if (typeof fun !== "function") {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
for (var i = 0; i < len; i++) {
if (i in t && fun.call(thisArg, t[i], i, t)) {
return true;
}
}
return false;
};
}
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.some |
Array.prototype.forEach()Array.prototype.every()Array.prototype.find()TypedArray.prototype.some()This page was last modified on 29 may 2026 by MDN contributors.
ArrayArray.prototype.at()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()Array.prototype.every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()findLastIndex()Array.prototype.flat()Array.prototype.flatMap()Array.prototype.forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()Array.prototype.reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()Array.prototype.some()Array.prototype.sort()Array.prototype.splice()Array.prototype.toLocaleString()toReversed()Array.prototype.toSorted()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()with()Array.prototype[@@iterator]()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |