JS 数组迭代方法
2013-06-20 11:14
561 查看
var arr = [3,4,5,6,7,"a"];
var isNum = function(elem,index,AAA){
return !isNaN(elem);
}
var toUpperCase = function(elem){
return String.prototype.toUpperCase.apply(elem);
}
var print = function(elem,index){
console.log(index+"."+elem);
}
/*对数组中的每一项执行测试函数,直到获得对指定的函数返回 false 的项。 使用此方法 可确定数组中的所有项是否满足某一条件,类似于&&的含义*/
var res = arr.every(isNum);
console.log(res);//false;
/*对数组中的每一项执行测试函数,直到获得返回 true 的项。 使用此方法确定数组中的所有项是否满足条件.类似于||的含义*/
res = arr.some(isNum);
console.log(res);//true
/*对数组中的每一项执行测试函数,并构造一个新数组,返回 true的项被添加进新数组。 如果某项返回 false,则新数组中将不包含此项*/
res = arr.filter(isNum);
console.log(res);//[3, 4, 5, 6, 7]
/*对数组中的每一项执行函数并构造一个新数组,并将原始数组中的每一项的函数结添加进新数组。*/
res = arr.map(toUpperCase);
console.log(res);//["3", "4", "5", "6", "7", "A"]
/*对数组中的每一项执行函数,不返回值*/
res = arr.forEach(print);
console.log(res);
//自己扩展
/*Array.prototype.every = function(fun,obj) {
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
for (var i = 0; i < len; i++) {
if (!fun.call(obj,this[i], i,this))
return false;
}
return true;
};*/
var isNum = function(elem,index,AAA){
return !isNaN(elem);
}
var toUpperCase = function(elem){
return String.prototype.toUpperCase.apply(elem);
}
var print = function(elem,index){
console.log(index+"."+elem);
}
/*对数组中的每一项执行测试函数,直到获得对指定的函数返回 false 的项。 使用此方法 可确定数组中的所有项是否满足某一条件,类似于&&的含义*/
var res = arr.every(isNum);
console.log(res);//false;
/*对数组中的每一项执行测试函数,直到获得返回 true 的项。 使用此方法确定数组中的所有项是否满足条件.类似于||的含义*/
res = arr.some(isNum);
console.log(res);//true
/*对数组中的每一项执行测试函数,并构造一个新数组,返回 true的项被添加进新数组。 如果某项返回 false,则新数组中将不包含此项*/
res = arr.filter(isNum);
console.log(res);//[3, 4, 5, 6, 7]
/*对数组中的每一项执行函数并构造一个新数组,并将原始数组中的每一项的函数结添加进新数组。*/
res = arr.map(toUpperCase);
console.log(res);//["3", "4", "5", "6", "7", "A"]
/*对数组中的每一项执行函数,不返回值*/
res = arr.forEach(print);
console.log(res);
//自己扩展
/*Array.prototype.every = function(fun,obj) {
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
for (var i = 0; i < len; i++) {
if (!fun.call(obj,this[i], i,this))
return false;
}
return true;
};*/
相关文章推荐
- js数组定义的5个迭代方法
- JS的数组迭代方法
- js 数组迭代方法,归并方法
- JS数组中every()与some()两者迭代方法
- JS的数组迭代方法
- 详谈JS中数组的迭代方法和归并方法
- 浅谈原生js的五个数组迭代方法
- js数组操作--使用迭代方法替代for循环
- 支持IE8及以下的,原生JS数组迭代的五种方法
- js数组操作--使用迭代方法替代for循环
- JS 数组迭代方法
- js相关数组迭代方法图解
- 关于JS数组中every与some两者迭代方法探索
- js中数组迭代的方法
- js数组的五种迭代方法及两种归并方法(推荐)
- js数组中的迭代方法
- js数组操作--使用迭代方法替代for循环
- JS原生数组迭代方法的应用
- JS数组迭代方法总结
- js将伪数组转换为标准数组的多种方法