JavaScript中的some()方法使用詳解
JavaScript數(shù)組some()方法測試數(shù)組中的某個元素是否通過由提供的功能來實現(xiàn)測試。
語法
array.some(callback[, thisObject]);
下面是參數(shù)的詳細(xì)信息:
- callback : 函數(shù)用來測試每個元素。
- thisObject : 對象作為該執(zhí)行回調(diào)時使用。
返回值:
如果某些元素通過測試則返回true,否則為false。
兼容性:
這個方法是一個JavaScript擴(kuò)展到ECMA-262標(biāo)準(zhǔn); 因此它可能不存在在標(biāo)準(zhǔn)的其他實現(xiàn)。為了使它工作,你需要添加下面的腳本代碼放到頂部:
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>
這將產(chǎn)生以下結(jié)果:
Returned value is : false Returned value is : true
- Vue.js報錯Failed to resolve filter問題的解決方法
- JavaScript 數(shù)組some()和filter()的用法及區(qū)別
- Some tips of wmi scripting in jscript (1)
- 詳解JavaScript中的every()方法
- jquery.fileEveryWhere.js 一個跨瀏覽器的file顯示插件
- 詳解JavaScript中的forEach()方法的使用
- js的for in循環(huán)和java里foreach循環(huán)的區(qū)別分析
- js中的for如何實現(xiàn)foreach中的遍歷
- 全面解析JavaScript里的循環(huán)方法之forEach,for-in,for-of
- 淺析JS中的 map, filter, some, every, forEach, for in, for of 用法總結(jié)
相關(guān)文章
JavaScript入門教程(10) 認(rèn)識其他對象
對于需要更好的控制html的一些元素,就需要了解這些了。大家知道就行,有需要時可以再看。2009-01-01JavaScript中的toDateString()方法使用詳解
這篇文章主要介紹了JavaScript中的toDateString()方法使用詳解,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06JavaScript?Hoisting變量提升機(jī)制實例解析
這篇文章主要為大家介紹了JavaScript變量提升Hoisting機(jī)制實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11