JavaScript array.some()方法测试数组中的某些元素是否满足条件。
array.some(callback[, thisObject]);
callback - 测试每个元素的回调函数。
thisObject - 要在执行回调时用作的对象。
如果某个元素通过测试,则返回TRUE,否则返回FALSE。
此方法是ECMA-262标准的JavaScript扩展;因此它可能不存在于该标准的其他实现中。要使其工作,您需要在脚本顶部添加以下代码。
链接:https://www.learnfk.comhttps://www.learnfk.com/javascript/array-some.html
来源:LearnFk无涯教程网
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; }; }
<html> <head> <title>JavaScript Array some Method</title> </head> <body> <script 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; }; } function isBigEnough(element, index, array) { return (element >= 10); } var retval=[2, 5, 8, 1, 4].some(isBigEnough); document.write("Returned value is : " + retval ); var retval=[12, 5, 8, 1, 4].some(isBigEnough); document.write("<br />Returned value is : " + retval ); </script> </body> </html>
运行上面代码输出
Returned value is : false Returned value is : true
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)