Javascript for 循环变量“i"被视为字符串? [英] Javascript for loop var "i" is treated as a string?
问题描述
我正在使用 Titanium 构建一些移动应用程序,但我注意到这会产生出乎我意料的结果.
I am using Titanium to build some mobile apps and I noticed that this will give a result that I wasn't expecting.
data = ['a','b', 'c','d'];
for (var i in data){
Ti.API.debug(i+1);
};
这将打印:01,11,12,13
This will print: 01,11,12,13
这是 Titanium 特有的东西还是 Javascript 中常见的东西?
Is this something particular to Titanium or is it generally in Javascript?
为什么不将i"视为整数?我很困扰.
Why isn't 'i' being treated as an integer? I am very confused.
感谢您的帮助.
推荐答案
这不能直接回答您的问题,但是如果您要循环遍历数组,则不使用 for (var i 在数据中)
.这会遍历对象的所有成员,包括方法、属性等.
This doesn't directly answer your question, but if you are looping through an array you should not use for (var i in data)
. This loops through all members of an object, including methods, properties, etc.
你想做的是:
for (var i=0, item; i<data.length; i++) {
item = data[i];
}
这篇关于Javascript for 循环变量“i"被视为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!