Looping through a JSON Array gives "undefined" results

This question already has an answer here: Why is using “for…in” with array iteration a bad idea? 25 answers JavaScript for…in vs for 23 answers 你忘记访问AlmostThere jsonData.TheArray[i].AlmostThere.whatWeAreLookingFor for (var i = 0; i < jsonData.TheArray.length; i++) { console.log(jsonData.TheArray[i].AlmostThere.whatWeAreLookingFor); } If you loop your array TheArray in your wa

通过JSON数组循环提供“未定义”结果

这个问题在这里已经有了答案: 为什么在数组迭代中使用“for ... in”是一个坏主意? 25个答案 JavaScript for ... in vs 23个答案 你忘记访问AlmostThere jsonData.TheArray[i].AlmostThere.whatWeAreLookingFor for (var i = 0; i < jsonData.TheArray.length; i++) { console.log(jsonData.TheArray[i].AlmostThere.whatWeAreLookingFor); } 如果以你的方式循环数组TheArray ,则contents var将成为这两个对象

Javascript why FOR IN is a bad practice?

Possible Duplicate: JavaScript “For …in” with Arrays People always tell me that using FOR IN is a bad practice, please could you tell me why? And why is better to use for with i? I always liked to use FOR IN because I use PHP too where I use foreach a lot and it's very similar to FOR IN in javascript :) Bad practice is not to understand the difference between enumerating over an arra

Javascript为什么FOR IN是不好的做法?

可能重复: JavaScript“For ... in”与数组 人们总是告诉我,使用FOR IN是一种不好的做法,请告诉我为什么? 为什么更好地使用我? 我总是喜欢使用FOR IN,因为我也使用PHP,在我使用foreach的地方也很多,它与JavaScript中的FOR IN非常相似:) 不好的做法是不了解枚举数组对象并迭代它的区别。 否则for...in循环中是非常好的工具。 有很大的内部差异,但我会尝试以实际的方式解释: 语法是: for (string in object)

JavaScript for loop syntax for array

I have seen the following style of writing a for loop in JavaScript a number of times: for(var i=0, n=myArray.length; i<n; i++) { // do something } what is the advantage of that over the following? for(var i=0; i<myArray.length; i++) { // do something } In theory, it only has to look up the length of myArray once, instead of each time it goes around the loop. In practise, these days I

用于数组的循环语法的JavaScript

我已经看到了多次用JavaScript编写for循环的下列风格: for(var i=0, n=myArray.length; i<n; i++) { // do something } 那对于以下有什么优势? for(var i=0; i<myArray.length; i++) { // do something } 理论上,它只需要查看一次myArray的长度,而不是每次绕过循环。 在实践中,我相信现在浏览器的JS引擎足够聪明,可以优化它们的性能影响。

does caching array length for a loop condition affect performance?

Is there a big difference between for (var i = 0, c = data.length; i < c; i++) { } And for (var i = 0; i < data.length; i++) { } What is the difference? In the first code, the length of the array(or array-like collection) is calculated only once and it it cached . So, the length is not recalculated per each iteration. While in the second code, the length is calculated per iterati

循环条件缓存数组长度是否会影响性能?

两者之间有很大差异吗? for (var i = 0, c = data.length; i < c; i++) { } 和 for (var i = 0; i < data.length; i++) { } 有什么不同? 在第一个代码中,数组的length (或类似数组的集合)只计算一次并将其缓存 。 所以,每次迭代都不重新计算长度。 在第二个代码中, 每次迭代计算长度 。 你可以说缓存长度比重新计算长度稍快。 这种差异非常小,您可以忽略小数组。 但是对于庞大阵列,差异可能很大。

Javascript array object arrays

THis is going to sound like a stupid question but here it goes. I have a js array formatted like so var locationID = [ { ID: "ID1", location: "location1" }, { ID: "ID2", location: "location2" }, { ID: "ID3", location: "location3" }, ]; I am trying to loop through the array for(i = 0; i < locationID.length;i++){ var object = locationID[i]; }

Javascript数组对象数组

这听起来像一个愚蠢的问题,但它在这里。 我有一个像这样格式化的js数组 var locationID = [ { ID: "ID1", location: "location1" }, { ID: "ID2", location: "location2" }, { ID: "ID3", location: "location3" }, ]; 我试图循环访问数组 for(i = 0; i < locationID.length;i++){ var object = locationID[i]; } 我想从内部数组获取两个元素,以便识别ID和位置。

Is the length of an array cached?

In JS are the lengths of an array cached or does it depend on the different engines/browsers? Normally I'd assume that browsers' JS engines are pretty dumb and cache the length of arrays, for example: var a = [ ]; var l = l; function arrayPush(i) { l = a.push( i ); } function arrayPop() { var r = a.pop(); l = a.length; return r; } (as a brief example, of

数组的长度是否缓存?

在JS中,缓存数组的长度还是取决于不同的引擎/浏览器? 通常我会假设浏览器的JS引擎非常笨,并缓存数组的长度,例如: var a = [ ]; var l = l; function arrayPush(i) { l = a.push( i ); } function arrayPop() { var r = a.pop(); l = a.length; return r; } (作为一个简单的例子,当然,复制每个数组函数都是愚蠢的,但如果它加快速度,那么它是值得的) 数组长度被缓存。 每次操作数

set length to variable or calculate length within loop statment

Is it more efficient to declare a variable with another variable's length before doing a for-loop or should the length be calculated within the loop. I'm certain the answer depends on how big the array is that the length is being calculated it on. I just always see for ( var i = 1; i <= theVar.length; i++ ) { } but then i was told that it is less resource intensive to do var the

将长度设置为变量或计算循环统计中的长度

在做for循环之前用另一个变量的长度声明一个变量是更有效的,或者应该在循环内计算长度。 我确定答案取决于数组的长度是多少以便计算长度。 我总是看到 for ( var i = 1; i <= theVar.length; i++ ) { } 但后来我被告知,这样做的资源密集程度较低 var theVarLen = theVar.length for ( var i = 1; i <= theVarLen; i++ ) { } 因为它避免了每次迭代重新计算长度。 或者它是否取决于具体的情况,所以不可能做出

In JavaScript, does the string/array .length property do any processing?

When iterating over a string or array (or anything else with a length property), I've always used a loop like this: var foo = [...]; var i; for(i=0; i<foo.length; i++) { // do something } However, I just encountered someone who did this: var foo = [...]; var fooLen = foo.length; var i; for(i=0; i<fooLen; i++) { // do something } He said he thought the ".length" was

在JavaScript中,字符串/数组.length属性是否执行任何处理?

当遍历一个字符串或数组(或其他具有length属性的东西)时,我总是使用这样的循环: var foo = [...]; var i; for(i=0; i<foo.length; i++) { // do something } 但是,我刚刚遇到了这样做的人: var foo = [...]; var fooLen = foo.length; var i; for(i=0; i<fooLen; i++) { // do something } 他说他认为“.length”正在重新计算长度,因此循环会重复计算字符串/数组的长度,所以通过将其长度保存为变量,它

JavaScript "cache" vs "memory"

If you search for how to cache a variable in JS on Stackoverflow, you'll find answers pointing to cookies or local storage for example. On the other hand, the word "cached" is often used as such: "cache the length of the array so we don't have to compute it every time". Surely we are not caching the length in cookies or local storage. My question is: Where is th

JavaScript“缓存”与“内存”

如果您搜索如何在Stackoverflow中的JS中缓存变量,则可以找到指向cookie或本地存储的答案。 另一方面,“缓存”这个词经常被用作:“缓存数组的长度,所以我们不必每次计算它”。 我们当然不会在cookie或本地存储中缓存长度。 我的问题是: “缓存”长度的位置在哪里? 它在记忆中吗? 如果是这样,为什么我们使用“缓存”这个词? 这是一个极其超负荷的问题,看起来你在这里混淆了一些公平的概念。 希望这有助于: 对你的

How to loop over an Array with JavaScript?

I have a string that has data separated by a pipe character ( | ). Example var somestring = "data1|data2|data3"; var separated = somestring.split("|"); I know how to use the split() to separate each data. However, I don't know how many pipes there will be in the resulting Array . In jQuery or JavaScript, how do I loop over the array returned? In jQuery or JavaScript, how do I loop t

如何使用JavaScript循环访问数组?

我有一个由管道字符( | )分隔数据的字符串。 例 var somestring = "data1|data2|data3"; var separated = somestring.split("|"); 我知道如何使用split()分离每个数据。 但是,我不知道在结果Array有多少个管道。 在jQuery或JavaScript中,我如何循环返回数组? 在jQuery或JavaScript中,我如何循环每个分离的变量? 你基本上只需要迭代结果Array 。 jQuery的 $.each循环 这种方法很容易使用,并且在被封装