用來測試數組的每一個元素的函數。調用時使用參數 (element, index, array)。
返回true表示保留該元素(經過測試),false則不保留。函數
可選。執行 callback 時的用於 this 的值。測試
一個新數組,每一個元素都是回調函數的結果。this
var arr = [1 , 2 , 3 , 4]; var thisArg = {name: 'grayVTouch'}; arr.forEach(function(val , index , arr){ arr[index] = val.toUpperCase(); console.log(this); // {name: 'grayVTouch'} } , thisArg); console.log(arr);
用來測試數組的每一個元素的函數。調用時使用參數 (element, index, array)。
返回true表示保留該元素(經過測試),false則不保留。code
可選。執行 callback 時的用於 this 的值。ip
一個新的經過測試的元素的集合的數組element
var arr = [1 , 2 , 3 , 4]; var thisArg = {name: 'grayVTouch'}; var res = arr.filter(function(val , index , arr){ console.log(this); // {name: 'grayVTouch'} if (val > 3) { return true; } return false; } , thisArg); console.log(arr); console.log(res);
用來測試數組的每一個元素的函數。調用時使用參數 (element, index, array)。
返回true表示保留該元素(經過測試),false則不保留。文檔
可選。執行 callback 時的用於 this 的值。get
一個新數組,每一個元素都是回調函數的結果。回調函數
var arr = [1 , 2 , 3 , 4]; var thisArg = {name: 'grayVTouch'}; var res = arr.map(function(val , index , arr){ console.log(this); // {name: 'grayVTouch'} return val + '數據測試'; } , thisArg); console.log(arr); console.log(res);