JavaScript中Array的filter函數(shù)詳解
描述
filter為數(shù)組中的每個(gè)元素調(diào)用一次callback函數(shù),并利用所有使得callback返回 true 或等價(jià)于 true 的值的元素創(chuàng)建一個(gè)新數(shù)組。callback只會在已經(jīng)賦值的索引上被調(diào)用,對于那些已經(jīng)被刪除或者從未被賦值的索引不會被調(diào)用。那些沒有通過callback 測試的元素會被跳過,不會被包含在新數(shù)組中。
理解
filter不會改變原數(shù)組,它返回過濾后的新數(shù)組。
filter遍歷的元素范圍在第一次調(diào)用callback之前就已經(jīng)確定了。在調(diào)用filter之后被添加到數(shù)組中的元素不會被filter遍歷到。如果已經(jīng)存在的元素被改變了,則他們傳入callback的值是filter遍歷到它們那一刻的值。被刪除或從來未被賦值的元素不會被遍歷到。
示例
過濾長度大于6的
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const result = words.filter(word => word.length > 6); console.log(result); // expected output: Array ["exuberant", "destruction", "present"]
使用
filter創(chuàng)建了一個(gè)新數(shù)組,該數(shù)組的元素由原數(shù)組中值大于 10 的元素組成。
function isBigEnough(element) {
return element >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44]使用
filter()根據(jù)搜索條件來過濾數(shù)組內(nèi)容
var fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
/**
* Array filters items based on search criteria (query)
*/
function filterItems(query) {
return fruits.filter(function(el) {
return el.toLowerCase().indexOf(query.toLowerCase()) > -1;
})
}
console.log(filterItems('ap')); // ['apple', 'grapes']
console.log(filterItems('an')); // ['banana', 'mango', 'orange']據(jù)搜索條件來過濾數(shù)組內(nèi)容
var fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
/**
* Array filters items based on search criteria (query)
*/
function filterItems(query) {
return fruits.filter(function(el) {
return el.toLowerCase().indexOf(query.toLowerCase()) > -1;
})
}
console.log(filterItems('ap')); // ['apple', 'grapes']
console.log(filterItems('an')); // ['banana', 'mango', 'orange']原生實(shí)現(xiàn)
filter 被添加到 ECMA-262 標(biāo)準(zhǔn)第 5 版中,因此在某些實(shí)現(xiàn)環(huán)境中不被支持??梢园严旅娴拇a插入到腳本的開頭來解決此問題,該代碼允許在那些沒有原生支持 filter 的實(shí)現(xiàn)環(huán)境中使用它。該算法是 ECMA-262 第 5 版中指定的算法,假定 fn.call 等價(jià)于 Function.prototype.call 的初始值,且 Array.prototype.push 擁有它的初始值。
if (!Array.prototype.filter){
Array.prototype.filter = function(func, thisArg) {
'use strict';
if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
throw new TypeError();
var len = this.length >>> 0,
res = new Array(len), // preallocate array
t = this, c = 0, i = -1;
if (thisArg === undefined){
while (++i !== len){
// checks to see if the key was set
if (i in this){
if (func(t[i], i, t)){
res[c++] = t[i];
}
}
}
}
else{
while (++i !== len){
// checks to see if the key was set
if (i in this){
if (func.call(thisArg, t[i], i, t)){
res[c++] = t[i];
}
}
}
}
res.length = c; // shrink down array to proper size
return res;
};
}到此這篇關(guān)于JavaScript中Array的filter函數(shù)詳解的文章就介紹到這了,更多相關(guān)JS filter 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript中this關(guān)鍵詞的使用技巧、工作原理以及注意事項(xiàng)
在JavaScript中,this 的概念比較復(fù)雜。除了在面向?qū)ο缶幊讨?,this 還是隨處可用的。這篇文章介紹了this 的工作原理,它會造成什么樣的問題以及this 的相關(guān)例子。2014-05-05
js表數(shù)據(jù)排序 sort table data
對于表格的排序,是很不錯(cuò)的一個(gè)功能,方便用戶快速的分析一些數(shù)據(jù)。2009-02-02
概述如何實(shí)現(xiàn)一個(gè)簡單的瀏覽器端js模塊加載器
本文主要對實(shí)現(xiàn)一個(gè)簡單的js加載器的步驟進(jìn)行介紹--主要可以分為解析路徑、下載模塊、解析模塊依賴、解析模塊四個(gè)步驟。需要的朋友來看下吧2016-12-12
使用bootstrap莫名其妙出現(xiàn)橫向滾動(dòng)條的問題及解決
這篇文章主要介紹了使用bootstrap莫名其妙出現(xiàn)橫向滾動(dòng)條的問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
JavaScript iframe 實(shí)現(xiàn)多窗口通信實(shí)例詳解
這篇文章主要為大家介紹了JavaScript iframe 實(shí)現(xiàn)多窗口通信實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
javascript中關(guān)于執(zhí)行環(huán)境的雜談
如你所知,javascript里執(zhí)行環(huán)境是作為一個(gè)最核心的概念存在的。相信廣大FE筒子們對于這個(gè)概念不會陌生,它定義了變量或函數(shù)有權(quán)訪問其他數(shù)據(jù)范圍以及其行為。2011-08-08
小程序安全指南之如何禁止外部直接跳轉(zhuǎn)到小程序某頁面
由于小程序跳轉(zhuǎn)的對象比較多,各自的規(guī)則又不一樣,因此小程序跳轉(zhuǎn)外部鏈接是用戶咨詢較多的問題之一,下面這篇文章主要給大家介紹了關(guān)于小程序安全指南之如何禁止外部直接跳轉(zhuǎn)到小程序某頁面的相關(guān)資料,需要的朋友可以參考下2022-09-09

