Execute an expression for every element in an array and determine whether one of the elements make the expression evaluate truthy. This polyfill adds the some
method to the array prototype for environments lower than ECMAScript 5.
-
-
Save eliperelman/1037838 to your computer and use it in GitHub Desktop.
Array.prototype.some polyfill for 140byt.es
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.some = [].some || // Use the native every method if available, otherwise: | |
function ( | |
a, // expression to test each element of the array against | |
b, // optionally change the 'this' context for the given callback | |
c, // placeholder iterator variable | |
d // placeholder variable (stores context of original array) | |
) { | |
for (c = 0, d = this; c < d.length; c++) // iterate over all of the array elements | |
if (a.call(b, d[c], c, d)) // call the given expression, passing in context, value, index, and original array | |
return !0; // if any expression evaluates true, immediately return since 'some' is true | |
return !1 // otherwise return false since all callbacks evaluated to false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.some=[].some||function(a,b,c,d){for(c=0,d=this;c<d.length;c++)if(a.call(b,d[c],c,d))return!0;return!1} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Eli Perelman <http://eliperelman.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "ArrayPrototypeSomePolyfill", | |
"description": "Array some polyfill method for ECMAScript versions older than 5.", | |
"keywords": [ | |
"Array", | |
"prototype", | |
"some", | |
"polyfill", | |
"ecmascript5" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>Array.prototype.some polyfill</title> | |
<script> | |
Array.prototype.some=[].some||function(a,b,c,d){for(c=0,d=this;c<d.length;c++)if(a.call(b,d[c],c,d))return!0;return!1} | |
var arr = [2,4,6,8,10,12,14,16]; | |
var arr2 = [1,2,3,4,5,6,7,8]; | |
// logs true, since at least 'some' elements are less than 10 | |
console.log(arr.some(function(value, index, myArray) { | |
return value < 10; | |
}); | |
// logs false, since none of the values are greater than 20 or none of arr2 values are greater than 10 | |
console.log(arr.some(function(value, index, myArray) { | |
// this refers to arr2, but myArray is still arr | |
return this[index] > 10 || value > 20; | |
}, arr2); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@atk nice, lots of space left for smth like (115 bytes):
which would avoid most problem keys but still allow keys like
"03"
... OR (125 bytes)which should avoid that edge case issue.