更新時間:2021-11-15 10:23:10 來源:動力節(jié)點(diǎn) 瀏覽864次
有不少剛進(jìn)行Java學(xué)習(xí)的小伙伴對于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í)行結(jié)果:
補(bǔ)充說明幾點(diǎn):
filter第一個函數(shù)參數(shù)的三個參數(shù)無須顯式傳遞,也不能顯式傳遞(會報(bào)參數(shù)未定義),在定義第一個函數(shù)參數(shù)(即config函數(shù))時這個函數(shù)的參數(shù)也必須嚴(yán)格按照filter的定義使用;
filter的第二個參數(shù)如未傳遞,則為默認(rèn)的undefined,也不能在第一個函數(shù)參數(shù)中使用(只有傳遞時才能使用);
filter第一個函數(shù)參數(shù)的返回值應(yīng)為布爾值,filter會根據(jù)這個返回值決定是否把當(dāng)前值(currentValue)放入最終的返回?cái)?shù)組(filterAr);
以上就是關(guān)于“舉例說明Filter用法”的介紹,如果大家對此比較感興趣,想了解更多相關(guān)知識,不妨來關(guān)注一下動力節(jié)點(diǎn)的Filter教程,里面的內(nèi)容豐富,通俗易懂,適合小白學(xué)習(xí),希望對大家能夠有所幫助。
初級 202925
初級 203221
初級 202629
初級 203743