小心FOR IN遍历数组
来源:互联网 发布:澳洲人工智能专业排名 编辑:程序博客网 时间:2024/06/05 17:40
From stackoverflow 查看原文
var a = []; // Create a new empty array.a[5] = 5; // Perfectly legal JavaScript that resizes the array.for (var i = 0; i < a.length; i++) { // Iterate over numeric indexes from 0 to 5, as everyone expects. console.log(a[i]);}/** * 输出: * undefined (5次) * 5 */
var a = [];a[5] = 5;for (var x in a) { // Shows only the explicitly set index of "5", and ignores 0-4 console.log(x);}/** * 输出: * 5 */
// Somewhere deep in your JavaScript library...Array.prototype.foo = 1;// Now you have no idea what the below code will do.var a = [1, 2, 3, 4, 5];for (var x in a){ // Now foo is a part of EVERY array and // will show up here as a value of 'x'. console.log(x);}/** * 输出: * 0 * 1 * 2 * 3 * 4 * foo */
加一个 hasOwnProperty (它可是JavaScript中唯一一个处理属性而不查找原型链的函数) 过滤一下还是可以:
if (a.hasOwnProperty(x)){ console.log(x);}
0 0
- 小心FOR IN遍历数组
- for...in遍历数组
- 使用for in遍历数组
- for in 遍历数组或对象注意事项
- for in语句对数组进行遍历
- javascript遍历数组切莫用for in
- for in数组遍历的坑
- for in 循环遍历对象和数组
- js中数组遍历for与for in区别(强烈建议不要使用for in遍历数组)
- js中数组遍历for与for in区别(强烈建议不要使用for in遍历数组)
- for in不要遍历数组,只能遍历对象
- javascript数组遍历for与for in区别详解
- js数组遍历for与for in区别
- JS数组的遍历方式for循环与for...in
- js 中的for in 与标准for循环遍历数组
- for-in循环和for循环遍历数组
- Actionscript 3遍历数组大集合 for、for(in)、 for each(in)
- for...in 遍历数组(转自W3School)
- 异常处理
- 扑克牌的顺子
- 理解java 序列化
- codeforces 733E (数学)
- Android产品研发(十五)-->内存对象序列化
- 小心FOR IN遍历数组
- 数据库:数据的查询
- LightOJ-1071-dp,dfs
- BFS广搜题目【经典训练题】
- ss kcptun安装及开机启动
- 消息队列的发送与接收
- 网络基础---TCP连接
- unity实现判断手指是否顺时针滑动实现简单的方向盘效果
- Spring的 编程式事务和声明式事务