更新時間:2021-11-15 10:23:10 來源:動力節(jié)點 瀏覽828次
有不少剛進行Java學習的小伙伴對于Filter用法還不是很了解,小編就來用例子說明filter的基本用法:
let ar = ["1", "2"];
let obj = {"key": "value"};
function config(currentValue, index, arr) {
console.log("currentValue is " + currentValue + " index is " + index + " arr is " + arr);
let that = this;
console.log("key is " + that.key);
if (currentValue === "1") {
return false;
}
return true;
}
let filterAr = ar.filter(config, obj);
console.log(JSON.stringify(filterAr));
貼一下執(zhí)行結果:
補充說明幾點:
filter第一個函數(shù)參數(shù)的三個參數(shù)無須顯式傳遞,也不能顯式傳遞(會報參數(shù)未定義),在定義第一個函數(shù)參數(shù)(即config函數(shù))時這個函數(shù)的參數(shù)也必須嚴格按照filter的定義使用;
filter的第二個參數(shù)如未傳遞,則為默認的undefined,也不能在第一個函數(shù)參數(shù)中使用(只有傳遞時才能使用);
filter第一個函數(shù)參數(shù)的返回值應為布爾值,filter會根據(jù)這個返回值決定是否把當前值(currentValue)放入最終的返回數(shù)組(filterAr);
以上就是關于“舉例說明Filter用法”的介紹,如果大家對此比較感興趣,想了解更多相關知識,不妨來關注一下動力節(jié)點的Filter教程,里面的內容豐富,通俗易懂,適合小白學習,希望對大家能夠有所幫助。