JavaScript中数组高级编程实践-2

来源:互联网 发布:java redis使用 编辑:程序博客网 时间:2024/06/12 01:40

我们来 看 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

运行结果:




1 0
原创粉丝点击