Array extensions in Javascript generally start with the object itself. Here we will introduce some items in the Array object. For example, indexOf is the index of the returned element in the Array, and-1 is returned if no element exists.
Recently I read something in developer.mozilla.org and found that it has added many generic methods to the Array Extension to catch up with Prototype's enthusiasm.
IndexOf
Returns the index of the element in the array. If no index exists,-1 is returned. Similar to the indexOf method of string.
If other browsers do not implement this method, use the following code for compatibility:
- Array.prototype.indexOf = function(el, start) {
- var startstart = start || 0;
- for ( var i=0; i < this.length; ++i ) {
- if ( this[i] === el ) {
- return i;
- }
- }
- return -1;
- };
- var array = [2, 5, 9];
- var index = array.indexOf(2);
- // index is 0
- index = array.indexOf(7);
- // index is -1
LastIndexOf
Similar to the lastIndexOf method of string.
If other browsers do not implement this method, use the following code for compatibility:
- Array.prototype.lastIndexOf = function(el, start) {
- var startstart = start || this.length;
- if ( start >= this.length ) {
- start = this.length;
- }
- if ( start < 0 ) {
- start = this.length + start;
- }
- for ( var i=start; i >= 0; --i ) {
- if ( this[i] === el ) {
- return i;
- }
- }
- return -1;
- };
ForEach
Similar each methods are implemented in various libraries.
If other browsers do not implement this method, use the following code for compatibility:
- Array.prototype.forEach = function(fn, thisObj) {
- var scope = thisObj || window;
- for ( var i=0, j=this.length; i < j; ++i ) {
- fn.call(scope, this[i], i, this);
- }
- };
- function printElt(element, index, array) {
- print("[" + index + "] is " + element); // assumes print is already defined
- }
- [2, 5, 9].forEach(printElt);
- // Prints:
- // [0] is 2
- // [1] is 5
- // [2] is 9
Every
If each element in the array can pass the test of the given function, true is returned, whereas false is returned. In other words, the given function must also return true and false.
If other browsers do not implement this method, use the following code for compatibility:
- Array.prototype.every = function(fn, thisObj) {
- var scope = thisObj || window;
- for ( var i=0, j=this.length; i < j; ++i ) {
- if ( !fn.call(scope, this[i], i, this) ) {
- return false;
- }
- }
- return true;
- };
- function isBigEnough(element, index, array) {
- return (element >= 10);
- }
- var passed = [12, 5, 8, 130, 44].every(isBigEnough);
- console.log(passed)
- // passed is false
- passed = [12, 54, 18, 130, 44].every(isBigEnough);
- // passed is true
- console.log(passed)
Some
Similar to the every function, but returns true if one test passes the given function.
If other browsers do not implement this method, use the following code for compatibility:
- Array.prototype.some = function(fn, thisObj) {
- var scope = thisObj || window;
- for ( var i=0, j=this.length; i < j; ++i ) {
- if ( fn.call(scope, this[i], i, this) ) {
- return true;
- }
- }
- return false;
- };
- 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
Filter
Place the elements that meet the conditions in a new array and return them.
If other browsers do not implement this method, use the following code for compatibility:
- Array.prototype.filter = function(fn, thisObj) {
- var scope = thisObj || window;
- var a = [];
- for ( var i=0, j=this.length; i < j; ++i ) {
- if ( !fn.call(scope, this[i], i, this) ) {
- continue;
- }
- a.push(this[i]);
- }
- return a;
- };
- function isBigEnough(element, index, array) {
- return (element <= 10);
- }
- var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
Map
Let each element in the array call the given function, and then put the result into the new array to return ..
If other browsers do not implement this method, use the following code for compatibility:
- Array.prototype.map = function(fn, thisObj) {
- var scope = thisObj || window;
- var a = [];
- for ( var i=0, j=this.length; i < j; ++i ) {
- a.push(fn.call(scope, this[i], i, this));
- }
- return a;
- };
- var numbers = [1, 4, 9];
- var roots = numbers.map(Math.sqrt);
- // roots is now [1, 2, 3]
- // numbers is still [1, 4, 9]
Reduce
Let the array element call the given function in sequence and return a value. In other words, the returned value must be used for the given function.
If other browsers do not implement this method, use the following code for compatibility:
- Array.prototype.reduce = function(fun /*, initial*/)
- {
- var len = this.length >>> 0;
- if (typeof fun != "function")
- throw new TypeError();
- if (len == 0 && arguments.length == 1)
- throw new TypeError();
- var i = 0;
- if (arguments.length >= 2){
- var rv = arguments[1];
- } else{
- do{
- if (i in this){
- rv = this[i++];
- break;
- }
- if (++i >= len)
- throw new TypeError();
- }while (true);
- }
- for (; i < len; i++){
- if (i in this)
- rv = fun.call(null, rv, this[i], i, this);
- }
- return rv;
- };
- var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; });
- // total == 6
Original article title: javascript Array Extension
Link: http://www.cnblogs.com/rubylouvre/archive/2009/09/20/1570461.html
- What is JSON? Data format prepared for JavaScript
- 10 most commonly used JavaScript udfs
- Some extended thoughts on loading JavaScript events
- Summary of JavaScript usage: From BOM and DOM
- How ExtJS works on Android Simulators