Array advanced programming practices in JavaScript-2
Let's take a look at the new APIs of arrays in the EcmaScript5 specification. They are very useful,
After introducing this part, we will use the Array object to construct a class similar to the ArrayList class in Java,
This allows you to encapsulate common logic and reuse code.
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) {};
Usage:
/*** @ Class MyEcmaScript5 * @ description * @ time * @ author StarZou *** // defines an array to store var array elements of different data types = [8, 2, 1, 5], array2 = [[0, 1], [1, 2], [2, 3], value; // Array. prototype. reduce = function (callback, initialValue) {}; // resolves the array: // calls the callback function provided by the reduce method, // calls the lenght-1 callback function of the array in total. // when the previusvalue is initialValue for the first time, // The returned value of the reduce method is previusvalue // reduceRight, which starts from the rightmost of the array, perform the preceding operations // Group Element Accumulation value = array. reduce (function (previousValue, currentValue, index, array) {return previusvalue + currentValue;}); console. log (value); // 16 // flat value = array2.reduce (function (previusvalue, currentValue, index, array) {return previusvalue. concat (currentValue) ;}); console. log (value); // [0, 1, 1, 2, 2, 3] // Array. prototype. indexOf = function (searchElement, fromIndex) {}; // from fromInde Search for the searchElement element at the index x and return its index; otherwise, the return value is-1 // fromIndex. The default value is 0console. log (array. indexOf (1); // 2console. log (array. indexOf (3); //-1 // Array. prototype. lastIndexOf = function (searchElement, fromIndex) {}; // search for the searchElement from the fromIndex index and return its index; otherwise, return-1console. log (array. lastIndexOf (1); // 2console. log (array. lastIndexOf (3); //-1 // Array. prototype. every = function (callback, thisObject) {}; // test Whether all elements have passed the test value of the specified function = array. every (function (element, index, array) {return element> 1 ;}); console. log (value); // false // Array. prototype. filter = function (callback, thisObject) {}; // array Filtering: // returns the elements tested by the function (when the callback function returns true, it indicates that the elements pass the test ), generate a new array value = array. filter (function (element, index, array) {return element> 1 ;}); console. log (value); // [8, 2, 5] // Array. prototype. forEach = function (callback, thi SObject) {}; // executes a callback function for each array element. Array. forEach (function (element, index, array) {console. log (element) ;}); // Array. prototype. map = function (callback, thisObject) {}; // array ing: // returns a new array consisting of the returned values after each element in the original array calls a specified method. Value = array. map (function (element, index, array) {return element * element;}); console. log (value); // [64, 4, 1, 25] // Array. prototype. some = function (callback, thisObject) {}; // test whether some elements in the array have passed the test of the specified function. Value = array. some (function (element, index, array) {return element> 1 ;}); console. log (value); // true // Array. prototype. isArray = function (object) {}; // checks whether the element is an array-type console. log (Array. isArray (array); // true
Running result: