JavaScript 教程
返回?cái)?shù)組 ages 中所有元素都大于 18 的元素:
輸出結(jié)果為:
filter() 方法創(chuàng)建一個(gè)新的數(shù)組,新數(shù)組中的元素是通過檢查指定數(shù)組中符合條件的所有元素。
注意: filter() 不會(huì)對(duì)空數(shù)組進(jìn)行檢測(cè)。
注意: filter() 不會(huì)改變?cè)紨?shù)組。
表格中的數(shù)字表示支持該方法的第一個(gè)瀏覽器的版本號(hào)。
方法 | |||||
---|---|---|---|---|---|
filter() | Yes | 9 | 1.5 | Yes | Yes |
array.filter(function(currentValue,index,arr), thisValue)
參數(shù) | 描述 | ||||||||
---|---|---|---|---|---|---|---|---|---|
function(currentValue, index,arr) | 必須。函數(shù),數(shù)組中的每個(gè)元素都會(huì)執(zhí)行這個(gè)函數(shù) 函數(shù)參數(shù):
|
||||||||
thisValue | 可選。對(duì)象作為該執(zhí)行回調(diào)時(shí)使用,傳遞給函數(shù),用作 "this" 的值。 如果省略了 thisValue ,"this" 的值為 "undefined" |
返回值: | 返回?cái)?shù)組,包含了符合條件的所有元素。如果沒有符合條件的元素則返回空數(shù)組。 |
---|---|
JavaScript 版本: | 1.6 |
返回?cái)?shù)組 ages 中所有元素都大于輸入框指定數(shù)值的元素: