JavaScript中數組進階編程實踐-2
我們來 看 EcmaScript5 規範中的 數組新的API ,它們是非常有用的,
介紹完這一部分 ,我們將用 Array 數組 這個對象 來構建 一個類似於Java中ArrayList 類,
以便於封裝 通用 的邏輯,實現代碼複用。
API :
/**@param {Function} callback@param {Object} [initialValue]@return {Object}*/Array.prototype.reduce = function(callback,initialValue) {};/**@param {Function} callback@param {Object} [initialValue]@return {Object}*/Array.prototype.reduceRight = function(callback,initialValue) {};/**@param {Object} searchElement@param {number} [fromIndex]@return {number}*/Array.prototype.indexOf = function(searchElement,fromIndex) {};/**@param {Object} searchElement@param {number} [fromIndex]@return {number}*/Array.prototype.lastIndexOf = function(searchElement,fromIndex) {};/**@param {Function} callback@param {Object} [thisObject]@return {boolean}*/Array.prototype.every = function(callback,thisObject) {};/**@param {Function} callback@param {Object} [thisObject]@return {Array}*/Array.prototype.filter = function(callback,thisObject) {};/**@param {Function} callback@param {Object} [thisObject]@return {void}*/Array.prototype.forEach = function(callback,thisObject) {};/**@param {Function} callback@param {Object} [thisObject]@return {Array}*/Array.prototype.map = function(callback,thisObject) {};/**@param {Function} callback@param {Object} [thisObject]@return {boolean}*/Array.prototype.some = function(callback,thisObject) {};/**@param {Object} object@return {boolean}*/Array.prototype.isArray = function(object) {};
使用:
/** *@class MyEcmaScript5 *@description *@time 2014-09-16 21:38 *@author StarZou **/// 定義一個數組,儲存不同資料類型的元素var array = [8, 2, 1, 5], array2 = [ [0, 1], [1, 2], [2, 3] ], value;/// Array.prototype.reduce = function(callback,initialValue) {};// 化解數組:// 調用reduce 方法提供的回呼函數,// 總共調用 數組的lenght - 1次 回呼函數// 第一次時 previousValue 為 initialValue// 此後 reduce方法的傳回值 作為 previousValue// reduceRight 則從數組最右邊開始,進行上述操作// 數組中元素累加value = array.reduce(function (previousValue, currentValue, index, array) { return previousValue + currentValue; });console.log(value); // 16// 數組扁平化value = array2.reduce(function (previousValue, currentValue, index, array) { return previousValue.concat(currentValue); });console.log(value); // [ 0, 1, 1, 2, 2, 3 ]/// Array.prototype.indexOf = function(searchElement,fromIndex) {};// 從fromIndex索引處 向後尋找searchElement元素 找到並返回其索引,否則返回-1// fromIndex 預設為 0console.log(array.indexOf(1)); // 2console.log(array.indexOf(3)); // -1/// Array.prototype.lastIndexOf = function(searchElement,fromIndex) {};// 從fromIndex索引處 向前尋找searchElement元素 找到並返回其索引,否則返回-1console.log(array.lastIndexOf(1)); // 2console.log(array.lastIndexOf(3)); // -1/// Array.prototype.every = function(callback,thisObject) {};// 測試數組的所有元素是否都通過了指定函數的測試value = array.every(function (element, index, array) { return element > 1;});console.log(value); // false/// Array.prototype.filter = function(callback,thisObject) {};// 數組過濾:// 返回通過函數測試的 元素(當回呼函數的返回true,表明元素通過測試 ),產生新的數組value = array.filter(function (element, index, array) { return element > 1;});console.log(value);// [ 8, 2, 5 ]/// Array.prototype.forEach = function(callback,thisObject) {};// 為每個數組元素執行一次回呼函數。array.forEach(function (element, index, array) { console.log(element);});/// Array.prototype.map = function(callback,thisObject) {};// 數組映射:// 返回一個由原數組中的每個元素調用一個指定方法後的傳回值組成的新數組。value = array.map(function (element, index, array) { return element * element;});console.log(value); // [ 64, 4, 1, 25 ]/// Array.prototype.some = function(callback,thisObject) {};// 測試數組中的某些元素是否通過了指定函數的測試。value = array.some(function (element, index, array) { return element > 1;});console.log(value); // true/// Array.prototype.isArray = function(object) {};// 判斷元素是否為數群組類型console.log(Array.isArray(array)); // true
運行結果: