问题标签 [for-in-loop]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
298 浏览

javascript - Javascript for-in 语句

我正在尝试在javascript中输出数组的键,如下所示:

但是,它不仅输出密钥,还输出以下内容:

为什么?输出数组键后如何让它停止?

0 投票
2 回答
234 浏览

delphi - FOR-IN loop that does not trigger due to it's range being not CONTINUOUS in delphi

My problem got simplified after a few days of tearing my hair out (left the important bits only, refer to my many edits for history), here it is:

#xA;

this is placed in a unit in my program of random fiddling with delphi. I think I should note that non-continuous for-in loop ranges work in the program elsewhere (another unit). In total, I have two such non-continuous for-in loops in the same procedure, if I comment out one the other works fine, but if they're both in working condition at the same time, only the second one works, the first one behaves like described below.

First messagebox contains "i= random-number" as it's not initialized, not that it needs to be. Messagebox with "1" in it never appears. Second messagebox that appears has "i=14", meaning that the loop did trigger but didnt do anything? That's ridiculous, if not, I want two things if someone can enlighten me:

1) why is this happening?

2) how to fix it and avoid in the future?

0 投票
1 回答
43 浏览

javascript - 这个程序是如何让我得到对象的属性值的?

基本上 for in 循环是如何工作的?

0 投票
5 回答
344 浏览

javascript - JS的for-in和in

当我in用于确定结果中的属性时,它返回 true;但是当我使用时for-in,结果中没有属性,为什么?

0 投票
2 回答
296 浏览

coldfusion - 如何在本地范围内将索引变量放入 for ... in 循环中?

每当我在循环中使用 for ... 时,循环的索引变量似乎总是在变量范围内。
例如,如果我有一个带有使用此循环的方法的组件:

变量'key'将被放置在变量范围内。如果我已经在组件的任何地方声明了 variables.key,那么当我在循环中使用这个 for ... 时,这个值会被覆盖。我真正需要的是这样的:

然而,这会引发解析错误。

有没有办法将 for ... in index 放在与变量范围不同的范围内?

0 投票
5 回答
1095 浏览

javascript - JavaScript“x in obj”:obj.x 未定义?

以下代码:

结果是:

现在,在for..in语句中使用相同的概念:

我希望它打印以下内容:

但它打印:

并且为了实现元素中的打印循环,它应该写成这样的数组元素:

那么,为什么第一个语法不起作用,但它在for..in语句之外起作用?

0 投票
4 回答
227 浏览

javascript - 为什么 JS for each in function 给太多了?

这困扰了我一段时间,请参阅我的 jsfiddle:http: //jsfiddle.net/DHR8Q/,其中包含以下 javascript 代码:

当我用字符“;”分割变量时id,然后我使用 for 循环,我会假设i == 0,1,2因为字符串id被分割成 3 部分,但这不是发生的情况。在现实i == 0,1,2,3,4...中。为什么 JavaScript 会这样做?

0 投票
2 回答
2867 浏览

ios5 - 在 NSInteger 中使用 for-in 循环?

我有一个用 NSIntegers 填充的 NSMutableArray。我需要遍历数组。我可以做:

但是,似乎 for (object in array) 循环会更干净。iOS 5 不接受 NSIntegers 或 NSNumbers 作为 for-in 循环中的对象。我是否应该使用 NSObjects 循环遍历数组,在每次迭代期间将 NSObject 转换为 NSInteger?还有其他方法吗?还是像上面那样的 for 循环是这个问题的最干净的解决方案?

0 投票
1 回答
2072 浏览

for-loop - awk for-in 循环给出意外的输出?

我目前正在awkbash 脚本中编写脚本。我的一个论点需要拆分和循环。例如:对于一个参数,1234我需要按写入的顺序循环遍历每个数字。所以,2413不等价。

我曾经split创建一个数组,然后创建一个for-in循环来循环遍历数组。我以为它会按顺序循环,但事实并非如此。

我的代码如下:

cols使用选项传递给awk命令-v并给出以下输出:

在测试了几次之后,使用不同长度的参数并使用数字和字母,for循环似乎从数组的第 4 个元素开始,按顺序循环到最后,然后循环遍历元素 1 到 3,而不是预期从元素 1 开始并循环到结束。

无论如何要改变行为还是我做错了什么?

编辑为了澄清,我gawk在 xubuntu 11.10 中使用

0 投票
4 回答
2923 浏览

javascript - Javascript:如何隐藏对象的属性?

我想让一个对象的一些属性被隐藏,一些在迭代时可见。所以我尝试使用原型,但这不起作用:

但这version也输出:

我真的很喜欢隐藏一些函数/方法但可以轻松访问它的想法car_instance.version,事实上,如果我 console.log 该对象在proto下也有其他属性,这些属性不包含在 for in 循环中。

那么我应该如何解决这个问题呢?