js 数组循环遍历
来源:互联网 发布:amazon跟卖软件 编辑:程序博客网 时间:2024/05/18 06:22
1. js 数组循环遍历。
数组循环变量,最先想到的就是 for(var i=0;i<COUNT;I++)这样的方式了。< p>
除此之外,也可以使用较简便的forEach 方式
2. forEach 函数。
Firefox 和Chrome 的Array 类型都有forEach的函数。使用如下:
[html
但是以上,代码在IE中却无法正常工作。
因为IE的Array 没有这个方法
[javascript]
alert(Array.prototype.forEach);
执行以上这句得到的是 "undefined", 也就是说在IE 中 Array 没有forEach的方法。
3. 让IE兼容forEach方法
既然IE的Array 没哟forEach方法, 我们就给它手动添加这个原型方法。
[javascript]
//Array.forEach implementation for IE support..
//https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, thisArg) {
var T, k;
if (this == null) {
throw new TypeError(" this is null or not defined");
}
var O = Object(this);
var len = O.length >>> 0; // Hack to convert O.length to a UInt32
if ({}.toString.call(callback) != "[object Function]") {
throw new TypeError(callback + " is not a function");
}
if (thisArg) {
T = thisArg;
}
k = 0;
while (k < len) {
var kValue;
if (k in O) {
kValue = O[k];
callback.call(T, kValue, k, O);
}
k++;
}
};
}
- js 数组循环遍历
- JS循环遍历JSON数组
- js遍历数组 循环json数组
- js 使用for循环遍历数组
- js for循环 遍历数组 遍历对象属性
- js数组循环遍历数组内所有元素
- js数组循环遍历数组内所有元素的方法
- JS数组循环遍历数组内所有元素的方法
- JS数组的遍历方式for循环与for...in
- js 中的for in 与标准for循环遍历数组
- js----循环遍历数组(键和值)
- for 循环遍历数组
- 循环遍历数组方法
- 循环遍历输出数组
- js中循环遍历
- js循环遍历
- js的循环遍历
- JS遍历打印、遍历数组
- pathogen插件管理器学习笔记
- leetcode OJ java Binary Tree Level Order Traversal
- Swift 自己写定时器
- Codeforces 469D Unbearable Controversy of Being
- 字符切割
- js 数组循环遍历
- 子类继承抽象父类的方法和抽象父类的子类的一些问题
- 【Poi2008】【BZOJ1113】海报PLA
- 今日学习札记——<string>模板类(11.17)
- 使用CocoaPods install 库之后如何 在工程中调用 新安装的库
- hdoj 5500 Reorder the Books
- Objective-C基础-对象和方法之间的联系
- CSS 元素垂直居中的 6种方法
- About ANR and OOM