Explanation of javascript some of Function Usage

  • 2021-08-03 09:22:37
  • OfStack

Parameter description
callback: The callback function to execute on each array element.
thisObject: The this object defined when the callback function is executed.

Function description
Executes the specified function (callback) once on each element in the array until it returns true, some returns true if this element is found, and some returns false if the callback function returns false after each element is executed. It only performs the specified function on non-empty elements in the array, and elements that have not been assigned or deleted will be ignored.

A callback function can take three arguments: the current element, the index of the current element, and the current array object.

If the parameter thisObject is passed in, it will be treated as the this object inside the callback function (callback), and if it is not passed or null, the global object will be used.


<script language="JavaScript" type="text/javascript">
if (!Array.prototype.some)
{
    Array.prototype.some = function(fun /*, thisp*/)
    {
        var len = this.length;
        if (typeof fun != "function")
            throw new TypeError();
        var thisp = arguments[1];
        for (var i = 0; i < len; i++)
        {
            if (i in this && fun.call(thisp, this[i], i, this))
                return true;
        }
        return false;
    };
}
</script>

some does not change the original array, Remember that only the array elements passed in before the callback function executes are valid, Elements added after the callback function starts execution will be ignored, and elements that are deleted or changed during the period from the callback function to the last 1 element will be ignored based on the time the callback function accessed the element.

Check whether all array elements are greater than or equal to 10


<script language="JavaScript" type="text/javascript">
if(!Array.prototype.some)
{
Array.prototype.some=function(fun)
{
var len=this.length;
if(typeof fun!="function")
throw new TypeError();
var thisp=arguments[1];for(var i=0;i<len;i++)
{
if(i in this&&fun.call(thisp,this[i],i,this))
return true;}
return false;};
}
function isBigEnough(element,index,array){return(element>=10);}
var passed=[2,5,8,1,4].some(isBigEnough);
document.writeln("[2, 5, 8, 1, 4].some(isBigEnough) : <strong>");
document.writeln(passed?'true':'false');
document.writeln("</strong><br />");
passed=[12,5,8,1,4].some(isBigEnough);
document.writeln("[12, 5, 8, 1, 4].some(isBigEnough) : <strong>");
document.writeln(passed?'true':'false');
document.writeln("</strong><br />");
</script>
function isBigEnough(element, index, array) {
 return (element >= 10);
}
var passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 4].some(isBigEnough);
// passed is true

Do you know something about some () function? If you have any questions, you can leave me a message


Related articles: