01 January 2015

Array.isArray(object)

描述:

判断一个对象是否为数组。

参数:

object:必需。需要判定的对象。

返回:

true or false

示例:

var arr1 = []
console.log(Array.isArray(arr1))
// true

var arr2 = new Array()
console.log(Array.isArray(arr2))
// true

var arr3 = [1,2,3,4]
console.log(Array.isArray(arr3))
// true

var arr4 = 'is Array'
console.log(Array.isArray(arr4))
// false

array1.indexOf(searchElement[, fromIndex])

描述:返回某个值在数组中第一个匹配项的索引。

参数:

array1:必需。一个数组对象
searchElement:必需。搜索的值。
fromIndex:可选。用于开始搜索的数组索引。如果省略 fromIndex,则从索引 0 处开始搜索。

返回:返回搜索的值在数组中第一个匹配的索引值,找不到,则返回-1

示例:

var arr = [1,2,3,3,2,1]
console.log(arr.indexOf(1))
// 0
console.log(arr.indexOf(4))
// -1
console.log(arr.indexOf(1,1))
// 5
console.log(arr.indexOf(1,-2))
// 5

向前兼容:

Array.prototype.indexOf = Array.prototype.indexOf || function(searchElement) {
    var len = this.length
    var i = +arguments[1] || 0 // fromIndex

    if(len === 0 || isNaN(i) || i>=len) {
        return -1
    }
    if(i<0) {
        i = len + i
        i<0 && i=0
    }
    for(; i<len; i++) {
        if( this.hasOwnProperty(String(i)) && this[i] ===  searchElement )
            return i
    }
    return -1
}

array1.lastIndexOf(searchElement[, fromIndex])

描述:

返回搜索值在数组中最后一个匹配项的索引。

参数:

array1:必需。一个数组对象。
searchElement:必需。搜索的值。
fromIndex:可选。用于开始搜索的数组索引。如果省略 fromIndex,则搜索将从数组中的最后一个索引处开始。

返回:

数组中的 searchElement 的最后一个匹配项的索引;如果未找到 searchElement,则为 -1。

示例:

var arr = [1,2,3,3,2,1]
console.log(arr.lastIndexOf(1))
// 5
console.log(arr.lastIndexOf(4))
// -1
console.log(arr.lastIndexOf(1,-2))
// 0

向前兼容:

Array.prototype.lastIndexOf = Array.prototype.lastIndexOf || function(searchElement) {
    var len = this.length
    var i = +arguments[1] || len-1 // fromIndex

    if(len === 0 || isNaN(i) || i>=len) {
        return -1
    }
    if(i<0) {
        i = len + i
    }else if(i>=len){
        i = len - 1
    }

    for(; i>=0; i--) {
        if( this.hasOwnProperty(String(i)) && this[i] ===  searchElement )
            return i
    }
    return -1
}

array1.every(callbackfn[, thisArg])

描述:

确定数组成员是否满足指定测试。

参数:

array1:必需。一个数组对象。
callbackfn:必需。一个接受最多三个参数的函数。every 方法会为 array1 中的每个元素调用 callbackfn 函数,直到 callbackfn 返回 false,或直到到达数组的结尾。
thisArg:可选。可在 callbackfn 函数中为其引用 this 关键字的对象。如果省略 thisArg,则 undefined 将用作 this 值。

返回值:

如果 callbackfn 函数为所有数组元素返回 true,则为 true;否则为 false。如果数组没有元素,则 every 方法将返回 true。

备注:

every 方法会按升序顺序对每个数组元素调用一次 callbackfn 函数,直到 callbackfn 函数返回 false。 如果找到导致 callbackfn 返回 false 的元素,则 every 方法会立即返回 false。 否则,every 方法返回 true。

回调函数语法

回调函数的语法如下所示:
function callbackfn(value, index, array1)
可使用最多三个参数来声明回调函数。
value:数组元素的值。
index:数组元素的数字索引。
array1:包含该元素的数组对象。

示例:

var arr = [1,2,3,4]
var result = arr.every(function(value) {
    return value>0
})
console.log(result)
// true

下面例子解释thisArg的用法:

var arr = [1,2,3,4]
var result = arr.every(function(value) {
    return value>this.zero
},{
    zero: 0
})
console.log(result)
// true

向前兼容:

var __isCallable = (function(){ 

    var __sortCase = (function(){
            try {
                [].sort('abc');
                return false;
            } catch(ex) {
                return true;
            }
        })();

    return function(obj){
        if( typeof obj === 'function' )
            return true;
        if( typeof obj !== 'object' ) 
            return false;
        if( obj instanceof Function || obj instanceof RegExp )
            return true;
        if( __sortCase ) {
            try {
                [].sort(obj);
                return true;
            } catch(ex){ /* nothing to do */ }
        }
        return false;
    };
})();

Array.prototype.every = Array.prototype.every || function(callback){
    if( !__isCallable(callback) )
        throw new TypeError( callback + " is not a callable object" );

    var thisArg = arguments[1]; 
    for(var i=0, len=this.length; i < len; i++) {
        if( this.hasOwnProperty(String(i)) ) {
            if( !callback.call(thisArg, this[i], i, this) )
                return false;
        }
    }

    return true;
};

array1.some(callbackfn[, thisArg])

描述:

确定数组成员是否满足指定测试。

参数:

array1:必需。一个数组对象。
callbackfn:必需。一个接受最多三个参数的函数。some 方法会为 array1 中的每个元素调用 callbackfn 函数,直到 callbackfn 返回 true,或直到到达数组的结尾。
thisArg:可选。可在 callbackfn 函数中为其引用 this 关键字的对象。如果省略 thisArg,则 undefined 将用作 this 值。

返回值:

如果 callbackfn 函数为任一数组元素返回 true,则为 true;否则为 false。

备注:

some 方法会按升序索引顺序对每个数组元素调用 callbackfn 函数,直到 callbackfn 函数返回 true。 如果找到导致 callbackfn 返回 true 的元素,则 some 方法会立即返回 true。 如果回调不对任何元素返回 true,则 some 方法会返回 false。

回调函数语法

回调函数的语法如下所示:
function callbackfn(value, index, array1)
可使用最多三个参数来声明回调函数。
value:数组元素的值。
index:数组元素的数字索引。
array1:包含该元素的数组对象。

示例:

var arr = [1,2,3,4]
var result = arr.some(function(value) {
    return value>2
})
console.log(result)
// true

下面例子解释thisArg的用法:

var arr = [1,2,3,4]
var result = arr.every(function(value) {
    return value>this.min
},{
    min: 2
})
console.log(result)
// true

向前兼容:

var __isCallable = (function(){ 

    var __sortCase = (function(){
            try {
                [].sort('abc');
                return false;
            } catch(ex) {
                return true;
            }
        })();

    return function(obj){
        if( typeof obj === 'function' )
            return true;
        if( typeof obj !== 'object' ) 
            return false;
        if( obj instanceof Function || obj instanceof RegExp )
            return true;
        if( __sortCase ) {
            try {
                [].sort(obj);
                return true;
            } catch(ex){ /* nothing to do */ }
        }
        return false;
    };
})();

Array.prototype.every = Array.prototype.every || function(callback){
    if( !__isCallable(callback) )
        throw new TypeError( callback + " is not a callable object" );

    var thisArg = arguments[1]; 
    for(var i=0, len=this.length; i < len; i++) {
        if( this.hasOwnProperty(String(i)) ) {
            if( callback.call(thisArg, this[i], i, this) )
                return false;
        }
    }

    return true;
};

array1.forEach(callbackfn[, thisArg])

描述:

为数组中的每个元素执行指定操作。

参数:

array1:必需。一个数组对象。
callbackfn:必需。最多可接受三个参数,为数组中的每个元素都执行一次callbackfn函数。
thisArg:可选。callbackfn 函数中的 this 关键字可引用的对象。如果省略 thisArg,则 undefined 将用作 this 值。

示例:

var arr = [1,2,3,4]

arr.forEach(funcion(value, index, arr) {
    console.log('value:'+value+', index:'+index)
})

// output
// value:1, index:0
// value:2, index:1
// value:3, index:2
// value:4, index:3

向前兼容:

Array.prototype.forEach = Array.prototype.forEach || function(callback){
    if( !__isCallable(callback) )
        throw new TypeError( callback + " is not a callable object" );

    var thisArg = arguments[1]; 
    for(var i=0, len=this.length; i < len; i++) {
        if( this.hasOwnProperty(String(i)) ) {
            callback.call(thisArg, this[i], i, this);
        }
    }       
}

array1.map(callbackfn[, thisArg])

描述:

对数组的每个元素调用定义的回调函数并返回包含结果的数组。

参数:

array1:必需。一个数组对象。
callbackfn:必需。最多可以接受三个参数的函数。 对于数组中的每个元素,map 方法都会调用 callbackfn 函数一次。
thisArg:可选。可在 callbackfn 函数中为其引用 this 关键字的对象。如果省略 thisArg,则 undefined 将用作 this 值。

返回值:

一个新数组,其中的每个元素均为关联的原始数组元素的回调函数返回值。

备注:

回调函数的语法如下所示:
function callbackfn(value, index, array1)
可使用最多三个参数来声明回调函数。
value:数组元素的值。
index:数组元素的数字索引。
array1:包含该元素的数组对象。

示例:

var arr = [1,2,3,4]
var result = arr.map(function(value) {
    return value*2
})
console.log(result)
// [2,4,6,8]

下面例子解释thisArg的用法:

var arr = [1,2,3,4]
var result = arr.map(function(value) {
    return value*this.number
},{
    min: 2
})
console.log(result)
// [2,4,6,8]

向前兼容:

Array.prototype.map = Array.prototype.map || function(callback){
    if( !__isCallable(callback) )
        throw new TypeError( callback + " is not a callable object" );

    var thisArg = arguments[1],
        len = this.length,
        results = new Array(len);
    for(var i=0; i < len; ++i) {
        if( this.hasOwnProperty(String(i)) ) {
            results[i] = callback.call(thisArg, this[i], i, this);
        }
    }

    return results;
};

array1.filter(callbackfn[, thisArg])

描述:

对数组的每个元素调用定义的回调函数并返回包含结果的数组。

参数:

array1:必需。一个数组对象。
callbackfn:必需。最多可以接受三个参数的函数。 对于数组中的每个元素,filter 方法都会调用 callbackfn 函数一次。
thisArg:可选。可在 callbackfn 函数中为其引用 this 关键字的对象。如果省略 thisArg,则 undefined 将用作 this 值。

返回值:

一个包含回调函数为其返回 true 的所有值的新数组。 如果回调函数为 array1 的所有元素返回 false,则新数组的长度为 0。

备注:

回调函数的语法如下所示:
function callbackfn(value, index, array1)
可使用最多三个参数来声明回调函数。
value:数组元素的值。
index:数组元素的数字索引。
array1:包含该元素的数组对象。

示例:

var arr = [1,2,3,4]
var result = arr.filter(function(value) {
    return value>2
})
console.log(result)
// [3,4]

下面例子解释thisArg的用法:

var arr = [1,2,3,4]
var result = arr.filter(function(value) {
    return value>this.min
},{
    min: 2
})
console.log(result)
// [3,4]

向前兼容:

Array.prototype.filter = Array.prototype.filter || function(callback){
    if( !__isCallable(callback) )
        throw new TypeError( callback + " is not a callable object" );

    var thisArg = arguments[1],
        len = this.length,
        results = [];
    for(var i=0; i < len; ++i) {
        if( this.hasOwnProperty(String(i)) ) {
            callback.call(thisArg, this[i], i, this) && results.push( this[i] );
            }
        }

    return results;
};

array1.reduce(callbackfn[, thisArg])

描述:

对数组中的所有元素调用指定的回调函数。该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供。

参数:

array1:必需。一个数组对象。
callbackfn:必需。一个接受最多四个参数的函数。 对于数组中的每个元素,reduce 方法都会调用 callbackfn 函数一次。
thisArg:可选。如果指定 initialValue,则它将用作初始值来启动累积。 第一次调用 callbackfn 函数会将此值作为参数而非数组值提供。

返回值:

通过最后一次调用回调函数获得的累积结果。

备注:

回调函数的语法如下所示:

function callbackfn(value, index, array1)
可使用最多三个参数来声明回调函数。
previousValue:通过上一次调用回调函数获得的值。 如果向 reduce 方法提供 initialValue,则在首次调用函数时,previousValue 为 initialValue。
currentValue:当前数组元素的值。
currentIndex:当前数组元素的数字索引。
array1:包含该元素的数组对象。

第一次调用回调函数:

在第一次调用回调函数时,作为参数提供的值取决于 reduce 方法是否具有 initialValue 参数。
如果向 reduce 方法提供 initialValue:

  • previousValue 参数为 initialValue。
  • currentValue 参数是数组中的第一个元素的值。

如果未提供 initialValue:

  • previousValue 参数是数组中的第一个元素的值。
  • currentValue 参数是数组中的第二个元素的值。

示例:

var arr = [1,2,3,4]
var result = arr.reduce(function(previousValue, currentValue) {
    return previousValue + currentValue
})
console.log(result)
// 10

下面例子解释initialValue的用法:

var arr = [1,2,3,4]
var result = arr.reduce(function(previousValue, currentValue) {
    return previousValue + currentValue
},5)
console.log(result)
// 15

向前兼容:

Array.prototype.reduce = Array.prototype.reduce || function(callback){
    if( !__isCallable(callback) )
        throw new TypeError( callback + " is not a callable object" );

    var len = this.length;
    if( len === 0 && arguments.length < 2 )
        throw new TypeError( "reduce of empty array with no initial value" );

    var initIdx = -1;
    if( arguments.length < 2 ) {
        if( (initIdx = __firstIndex(this)) === -1 )
            throw new TypeError( "reduce of empty array with no initial value" );               
    }

    var val = arguments.length > 1 ? arguments[1] : this[initIdx];

    for(var i=initIdx+1; i < len; i++) {
        if( this.hasOwnProperty(String(i)) ) {
            val = callback(val, this[i], i, this);
        }
    }

    return val;
};

array1.reduceRight(callbackfn[, thisArg])

描述:

按降序顺序对数组中的所有元素调用指定的回调函数。 该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供。

参数:

array1:必需。一个数组对象。
callbackfn:必需。一个接受最多四个参数的函数。 对于数组中的每个元素,reduce 方法都会调用 callbackfn 函数一次。
thisArg:可选。如果指定 initialValue,则它将用作初始值来启动累积。 第一次调用 callbackfn 函数会将此值作为参数而非数组值提供。

返回值:

通过最后一次调用回调函数获得的累积结果。

备注:

回调函数的语法如下所示:

function callbackfn(value, index, array1)
可使用最多三个参数来声明回调函数。
previousValue:通过上一次调用回调函数获得的值。 如果向 reduce 方法提供 initialValue,则在首次调用函数时,previousValue 为 initialValue。
currentValue:当前数组元素的值。
currentIndex:当前数组元素的数字索引。
array1:包含该元素的数组对象。

第一次调用回调函数:

在第一次调用回调函数时,作为参数提供的值取决于 reduce 方法是否具有 initialValue 参数。
如果向 reduce 方法提供 initialValue:

  • previousValue 参数为 initialValue。
  • currentValue 参数是数组中的最后一个元素的值。

如果未提供 initialValue:

  • previousValue 参数是数组中的最后一个元素的值。
  • currentValue 参数是数组中的倒数第二个元素的值。

示例:

var arr = [1,2,3,4]
var result = arr.reduceRight(function(previousValue, currentValue) {
    return previousValue + currentValue
})
console.log(result)
// 10

下面例子解释initialValue的用法:

var arr = [1,2,3,4]
var result = arr.reduceRight(function(previousValue, currentValue) {
    return previousValue + currentValue
},5)
console.log(result)
// 15

向前兼容写法:

Array.prototype.reduceRight = Array.prototype.reduceRight || function(callback){
    if( !__isCallable(callback) )
        throw new TypeError( callback + " is not a callable object" );

    var len = this.length;
    if( len === 0 && arguments.length < 2 )
        throw new TypeError( "reduce of empty array with no initial value" );

    var initIdx = len;
    if( arguments.length < 2 ) {
        for( var k=len-1; k >=0; --k ) {
            if( this.hasOwnProperty(String(k)) ) {
                initIdx = k;
                break;
            }
        }
        if( initIdx === len )
            throw new TypeError( "reduce of empty array with no initial value" );               
    }       

    var val = arguments.length > 1 ? arguments[1] : this[initIdx];

    for(var i=initIdx-1; i >= 0; --i) {
        if( this.hasOwnProperty(String(i)) ) {
            val = callback(val, this[i], i, this);
        }
    }

    return val;
};

如果有任何问题都可以在下方给予我留言~