标签:
1.Array.isArray()
The Array.isArray() method returns true if an object is an array,false if it is not.
如果判断的对象是一个数组则返回true,否则返回false;
实现:
if (!Array.isArray) {
Array.isArray = function(arg){
return Object.prototype.toString.call(arg) === ‘[object Array]‘;
}
};
The Array.of() method creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.
Array.of()方法创建一个新的参数个数可变数组实例,而这个新的数组会忽视参数的数量和类型
实现:
if (!Array.of) {
Array.of = function() {
return Array.prototype.slice.call(arguments);
};
}
eg: Array.of(1,2,3) //[1,2,3]
3.Array.prototype.concat()
concat() 方法用于连接两个或多个数组。该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本
var num1 = [1, 2, 3],
num2 = [4, 5, 6],
num3 = [7, 8, 9];
var nums = num1.concat(num2, num3);
console.log(nums); // Result: [1, 2, 3, 4, 5, 6, 7, 8, 9] 4.
Array.prototype.entries()(新特性)
The entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
返回数组元素的索引和值
var arr = [‘a‘, ‘b‘, ‘c‘]; var eArr = arr.entries();//数组迭代器arrayIterator console.log(eArr.next().value); // [0, ‘a‘] console.log(eArr.next().value); // [1, ‘b‘] console.log(eArr.next().value); // [2, ‘c‘]5. Array.prototype.every()
The every() method tests whether all elements in the array pass the test implemented by the provided function.
测试数组中的所有元素是否满足要求,如果满足返回true,否则返回false
if (typeof Array.prototype.every != "function") {
Array.prototype.every = function (fn, context) {
var passed = true;
if (typeof fn === "function") {
for (var k = 0, length = this.length; k < length; k++) {
if (passed === false) break;
passed = !!fn.call(context, this[k], k, this);
}
}
return passed;
};
}
var scores = [5, 8, 3, 10];
var current = 7;
function test(score) {
return score > current;
}
if (scores.every(test)) {
console.log("满足!");
} else {
console.log("不满足!");
} 6.
Array.prototype.filter()
filter() 方法利用所有通过指定函数测试的元素创建一个新的数组,并返回
function isBigEnough(element) {
return element >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44] filter不会改变原数组。
callback被调用时传入三个参数:
7.Array.prototype.map()
map()方法返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组。
function fuzzyPlural(single) {
var result = single.replace(/o/g, ‘e‘);
if( single === ‘kangaroo‘){
result += ‘se‘;
}
return result;
}
var words = ["foot", "goose", "moose", "kangaroo"];
console.log(words.map(fuzzyPlural));
// ["feet", "geese", "meese", "kangareese"]
var numbers = [1, 4, 9]; var roots = numbers.map(Math.sqrt); /* roots的值为[1, 2, 3], numbers的值仍为[1, 4, 9] */
通常情况下,map 方法中的 callback 函数只需要接受一个参数,就是正在被遍历的数组元素本身。但这并不意味着 map 只给 callback 传了一个参数。这个思维惯性可能会让我们犯一个很容易犯的错误。
// 下面的语句返回什么呢:
["1", "2", "3"].map(parseInt);
// 你可能觉的会是[1, 2, 3]
// 但实际的结果是 [1, NaN, NaN]
// 通常使用parseInt时,只需要传递一个参数.但实际上,parseInt可以有两个参数.第二个参数是进制数.可以通过语句"alert(parseInt.length)===2"来验证.
// map方法在调用callback函数时,会给它传递三个参数:当前正在遍历的元素, 元素索引, 原数组本身.
// 第三个参数parseInt会忽视, 但第二个参数不会,也就是说,parseInt把传过来的索引值当成进制数来使用.从而返回了NaN.
/*
//应该使用如下的用户函数returnInt
function returnInt(element){
return parseInt(element,10);
}
["1", "2", "3"].map(returnInt);
// 返回[1,2,3]
*/
标签:
原文地址:http://my.oschina.net/u/2328177/blog/487586