问题标签 [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 回答
3948 浏览

javascript - JavaScript“for in”循环的问题

我有一组对象,它们将成为我网站中某个菜单的基础。它将使用 JavaScript 构建:

所以我决定使用“for in 循环”,这样我就不必处理索引和长度了。我希望在构建时菜单中会出现七个项目(我将使用<ul>and <li>)。

当我在调试时不小心为<li>. <li>在可见的第 7 个菜单之后,我发现至少有 30 个是空的<li>

为什么会这样?

编辑:

这是循环。该循环为稍后解析的另一个函数创建另一个对象。(它创建了一个<li>带有<a>前一个数组提供的属性的内部。)我知道另一个函数可以正常工作,因为当我将此“for-in”循环更改为普通的 for 循环或 while 循环时,它可以正常工作。

0 投票
3 回答
260 浏览

javascript - Javascript closures issues

So, I'm still reading Apress Pro Javascript Techniques and i'm having troubles with closures.

As John Resig states:

Closures allow you to reference variables that exist within the parent function. However it does not provide the value of the variable at the time it is created; It provides the last value of the variable withing the parent function. The most common issue under which you'll see this occurr during a for loop. There is one variable being used as an interaor (e.g., i). Inside of the for loop, new functions are being created that utilize the closure to reference the iterator again. The rpoblem is tat the time the new closured functions are called, they will reference the last value of the iterator (i.e., the last position in an array), not the value taht you woul expect.

Then he presents, in listing 2-16 an example using anonymous functions to induce scope.

This example works as expected, and the behavious of the main object is correct.

The in the following, it uses another time a self-executing function to induce scope, during an iteration.

The purpose of the function is to create an object, defining getters and setters for all its properties. In this case, the example does not work.

Instead, after passing the i parameter as argument to the self-executing function,it works.

My question is:

  • Why in the first case (for loop), it is not necessary to pass the i parameter, while
    in the second (for in) it is needed in order to work properly?
0 投票
2 回答
444 浏览

javascript - 使用 for-in 循环在 javascript 中应用样式

所以我有一个充满键值对的对象,这些键值对描述了元素的预期样式,我试图通过像这样循环遍历对象来将该样式应用于元素:

然而,该元素仍然没有样式。这似乎很简单,以至于我无法弄清楚可能出了什么问题,所以我认为我遗漏了一些非常明显的东西。感谢您提供任何帮助。

0 投票
2 回答
165 浏览

javascript - 是否可以遍历一个对象并获取键名($key => $value)?

在 PHP 中,我们可以遍历关联数组,并获取键和值的值,如下所示:

有什么办法可以在javascript中完成吗?

0 投票
1 回答
52649 浏览

objective-c - For...in 语句 Objective-C

我正在学习Objective-C,我遇到了这个“for...in”语句。我搜索了它,但我仍然不明白它是如何工作的。有人可以用对菜鸟友好的方式向我解释这个语句是如何工作的吗?

0 投票
3 回答
1333 浏览

javascript - 迭代 String.prototype

我知道for in循环可以帮助遍历对象、原型和集合的属性。

事实是,我需要迭代String.prototype,虽然console.log(String.prototype)显示完整的原型,但当我这样做时

要显示原型中元素的名称,它什么也不显示,就好像它是空的一样。

JavaScript 引擎是否隐藏了基本的原型方法,还是我做错了什么?

0 投票
1 回答
266 浏览

objective-c - 枚举 NSMutableDictionary - 无法从循环内访问对象属性

我有一个 NSMutableDictionary, analyzedPxDictionary,其中包含一堆 Pixel 对象(我创建的自定义类)。除其他外,Pixel 对象包含一个名为 的 NSArray 属性rgb。该数组将始终包含三个 NSNumber 对象,其整数值对应于像素的 rgb 值。

我现在正在尝试枚举analyzedPxDictionaryusing 快速枚举。但是,似乎我无法从循环中访问 Pixel 对象的属性。我已经声明rgb它是一个综合属性,以便我可以使用点语法访问它。但是当我尝试从循环中执行此操作时,程序崩溃,给我一个如下错误:

'-[NSCFString rgb]: unrecognized selector sent to instance 0xa90bb50'

这是产生该错误的代码示例:

我尝试在该printf行上设置一个断点以检查px. 如果rgb它的属性被列为一个并且被正确描述为 NSArray 的实例,它似乎不包含任何对象。

我相信我正在rgb正确初始化。为了解释,请考虑以下代码:

这成功地将正确的值打印到控制台。

那么为什么我不能rgbforin循环内访问该属性呢?

0 投票
2 回答
17477 浏览

javascript - javascript "for (x in y)" 语句

我想知道,我必须假设什么而不是“x”。

0 投票
5 回答
288 浏览

javascript - for... in 循环在 JavaScript 中有用吗?

我是 JavaScript 新手,目前正在学习所谓的for... in循环。

在 JavaScript 中编码时是否真的使用了这些循环?

我可以看到所有其他类型的循环是如何有用的——但不是这个。

请有人对此有所了解,并在可能的情况下包括一个真实的例子。

0 投票
2 回答
4268 浏览

javascript - Javascript中的“对象不支持此属性或方法IE”错误可能来自使用hasOwnProperty?

由于某种原因,我在 IE8 和 IE7 中遇到了这个错误。我正在遍历对象中的所有键,它一直在告诉我Object doesn't support this property or method

整个区块如下:

有人在 Doug Crockford 的 jslint 板上发布了这个,他回复说:

for (key in object) { if (Object.prototype.hasOwnProperty.call(object, key)) { ... } }

hasOwnProperty 应该是一个操作符,而不是一个方法,因为作为一个方法,它很容易出现这些问题。但它就是这样,所以你必须解决这个问题。

像往常一样在 Chrome、FF、Safari、Opera 等中正常工作。关于解决方法的任何想法?