for..in 循环遍历 non-numeric 个索引“clean”和“remove”
for..in loop loops over non-numeric indexes “clean” and “remove”
这是我可能在这里遗漏的非常基本的东西,但直到现在我还没有看到这样的结果。
我有一个 for 循环,其中 options.headers.length 是 3
。在 for 循环中,我正在动态创建一个 table header。理想情况下,此循环应该 运行 三次 0 1 and 2
,但是当我打印索引时,它正在打印 0,1,2,clean and remove
。我还没有看到 clean and remove
作为索引。我知道这些信息还不够,但如果您有任何线索,请提出建议。某些东西可能会覆盖这也是我在调试后得出的结论。
for (index in options.headers)
如果您不想迭代 clean
和 remove
则将循环更改为:
for (var i=0; i< options.headers.length;i++){
//use i for getting the array data
}
如果您使用 for (index in options.headers)
,它也会迭代 non-numeric
个键。
我假设 options.headers 是一个数组?
当您(或您加载的某些框架)向 Array 原型添加方法时会发生这种情况。 "for in" 循环还将枚举这些添加的方法。因此你应该为一个数组做循环:
for (var i = 0; i < options.headers.length; i++)
这样你只会得到真正的值,而不是添加的方法。
不要只使用索引(因为那是 = window.index = global = bad)使用 var index
(在此处阅读更多内容 https://www.google.pl/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=globals+javascript+bad)
你必须检查数组是否有它自己的 属性 或者它的一些功能(答案后更多)
for (var index in options.headers) {
if (options.headers.hasOwnProperty(index) {
// code here
}
}
关于 #2 的更多信息:
假设我们有
var array = [0,1,2,3];
除此之外,用函数扩展数组(数组在 javascript 和字符串中也可以有函数)
Array.prototype.sayHello = function() {
alert('Hello');
};
然后你的循环会打印 sayHello 作为数组的一部分,但那不是它自己的 属性,只有数组
这是我可能在这里遗漏的非常基本的东西,但直到现在我还没有看到这样的结果。
我有一个 for 循环,其中 options.headers.length 是 3
。在 for 循环中,我正在动态创建一个 table header。理想情况下,此循环应该 运行 三次 0 1 and 2
,但是当我打印索引时,它正在打印 0,1,2,clean and remove
。我还没有看到 clean and remove
作为索引。我知道这些信息还不够,但如果您有任何线索,请提出建议。某些东西可能会覆盖这也是我在调试后得出的结论。
for (index in options.headers)
如果您不想迭代 clean
和 remove
则将循环更改为:
for (var i=0; i< options.headers.length;i++){
//use i for getting the array data
}
如果您使用 for (index in options.headers)
,它也会迭代 non-numeric
个键。
我假设 options.headers 是一个数组?
当您(或您加载的某些框架)向 Array 原型添加方法时会发生这种情况。 "for in" 循环还将枚举这些添加的方法。因此你应该为一个数组做循环:
for (var i = 0; i < options.headers.length; i++)
这样你只会得到真正的值,而不是添加的方法。
不要只使用索引(因为那是 = window.index = global = bad)使用 var index (在此处阅读更多内容 https://www.google.pl/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=globals+javascript+bad)
你必须检查数组是否有它自己的 属性 或者它的一些功能(答案后更多)
for (var index in options.headers) {
if (options.headers.hasOwnProperty(index) {
// code here
}
}
关于 #2 的更多信息:
假设我们有
var array = [0,1,2,3];
除此之外,用函数扩展数组(数组在 javascript 和字符串中也可以有函数)
Array.prototype.sayHello = function() {
alert('Hello');
};
然后你的循环会打印 sayHello 作为数组的一部分,但那不是它自己的 属性,只有数组