Js 數組——filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()

filter():
語法:

var filteredArray = array.filter(callback[, thisObject]);

參數說明:

callback: 要對每個數組元素執行的回調函數。
thisObject : 在執行回調函數時定義的this對象。

//過濾掉小於 10 的數組元素:

//代碼:
function isBigEnough(element, index, array) {
    return (element >= 10);
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// 12, 130, 44
//結果:[12, 5, 8, 130, 44].filter(isBigEnough) : 12, 130, 44 

功能說明:

對數組中的每個元素都執行一次指定的函數(callback),並且創建一個新的數組,該數組元素是所有回調函數執行時返回值爲 true 的原數組元素。它只對數組中的非空元素執行指定的函數,沒有賦值或者已經刪除的元素將被忽略,同時,新創建的數組也不會包含這些元素。

回調函數可以有三個參數:當前元素,當前元素的索引和當前的數組對象。

如參數 thisObject 被傳遞進來,它將被當做回調函數(callback)內部的 this 對象,如果沒有傳遞或者爲null,那麼將會使用全局對象。

filter 不會改變原有數組,記住:只有在回調函數執行前傳入的數組元素纔有效,在回調函數開始執行後才添加的元素將被忽略,而在回調函數開始執行到最後一個元素這一期間,數組元素被刪除或者被更改的,將以回調函數訪問到該元素的時間爲準,被刪除的元素將被忽略。

map():

//將所有的數組元素轉換爲大寫:

var strings = ["hello", "Array", "WORLD"];
function makeUpperCase(v)
{
    return v.toUpperCase();
}
var uppers = strings.map(makeUpperCase);
// uppers is now ["HELLO", "ARRAY", "WORLD"]
// strings is unchanged
//結果:["hello", "Array", "WORLD"].map(makeUpperCase) : HELLO, ARRAY, WORLD 

some():
對數組中的每個元素都執行一次指定的函數(callback),直到此函數返回 true,如果發現這個元素,some 將返回 true,如果回調函數對每個元素執行後都返回 false ,some 將返回 false。它只對數組中的非空元素執行指定的函數,沒有賦值或者已經刪除的元素將被忽略。

//檢查是否有數組元素大於等於10:

function isBigEnough(element, index, array) {
    return (element >= 10);
}
var passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 4].some(isBigEnough);
// passed is true
//結果:
//[2, 5, 8, 1, 4].some(isBigEnough) : false 
//[12, 5, 8, 1, 4].some(isBigEnough) : true 

every():
對數組中的每個元素都執行一次指定的函數(callback),直到此函數返回 false,如果發現這個元素,every 將返回 false,如果回調函數對每個元素執行後都返回 true ,every 將返回 true。它只對數組中的非空元素執行指定的函數,沒有賦值或者已經刪除的元素將被忽略

//測試是否所有數組元素都大於等於10:

function isBigEnough(element, index, array) {
    return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true
//結果:
//[12, 5, 8, 130, 44].every(isBigEnough) 返回 : false 
//[12, 54, 18, 130, 44].every(isBigEnough) 返回 : true 

forEach():

//打印數組內容:

function printElt(element, index, array) {
    document.writeln("[" + index + "] is " + element + "<br />");
}
[2, 5, 9].forEach(printElt);
// Prints:
// [0] is 2
// [1] is 5
// [2] is 9
//結果:
//[0] is 2
//[1] is 5
//[2] is 9

lastIndexOf():
語法

var index = array.lastIndexOf(searchElement[, fromIndex]);

參數說明

searchElement: 要搜索的元素

fromIndex : 開始搜索的位置,默認爲數組的長度(length),在這樣的情況下,將搜索所有的數組元素。搜索是反方向進行的。

功能說明

比較 searchElement 和數組的每個元素是否絕對一致(===),當有元素符合條件時,返回當前元素的索引。如果沒有發現,就直接返回 -1 。

//查找符合條件的元素:

var array = [2, 5, 9, 2];
var index = array.lastIndexOf(2);
// index is 3
index = array.lastIndexOf(7);
// index is -1
index = array.lastIndexOf(2, 3);
// index is 3
index = array.lastIndexOf(2, 2);
// index is 0
index = array.lastIndexOf(2, -2);
// index is 0
index = array.lastIndexOf(2, -1);
// index is 3
//結果:
//[2, 5, 9, 2].lastIndexOf(2) : 3 
//[2, 5, 9, 2].lastIndexOf(7) : -1 
//[2, 5, 9, 2].lastIndexOf(2, 3) : 3 
//[2, 5, 9, 2].lastIndexOf(2, 2) : 0 
//[2, 5, 9, 2].lastIndexOf(2, -2) : 0 
//[2, 5, 9, 2].lastIndexOf(2, -1) : 3 

indexOf():
功能與lastIndexOf()一樣,搜索是正向進行的

//查找符合條件的元素:

var array = [2, 5, 9];
var index = array.indexOf(2);
// index is 0
index = array.indexOf(7);
// index is -1
//結果:
//[2, 5, 9].indexOf(2) : 0 
//[2, 5, 9].indexOf(7) : -1 

轉載自 http://www.cnblogs.com/xiao-hong/p/3194027.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章