鸡肉和鸡肉通过JSON列表子集迭代的蛋问题 [英] Chicken & Egg Problem Iterating Through Subset of JSON List

查看:57
本文介绍了鸡肉和鸡肉通过JSON列表子集迭代的蛋问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我不断努力生产更短,更高效的代码时,我创造了一个鸡肉和鸡蛋。 /catch-22问题。我可以想到几种方法来解决这个问题,它们都不优雅。


我希望我的代码声明var stop 如果它没有传递给函数。问题是 stop 将等于依赖于尚未声明的var index 的值,但是 index 直到<我宣布停止。所以你看到了我的鸡蛋和鸡蛋问题。


在下面第30行,你可以看到我声明停止当它是'未定义'时。但是, index 直到第42行才被声明。那么,如何处理这个问题呢?在你问之前,不,我还没有运行这个代码,因为我知道它不会按照它的编写方式工作。我不是 密集的。 = D

展开 | 选择 | Wrap | < span class =codeLinkonclick =LineNumbers(this);>行号

解决方案

heya ...

所以看看下面的代码...我让我工作测试并简化它,因为我可以再次按照你的解释:) ...目前下面的代码将产生:

展开 | 选择 | Wrap | 行号


只是一边注意:当你使用数组时,你必须清楚自己。当你使用对象的属性时。在JS中,只有在使用整数索引时才会创建数组。否则它只是对象'的属性(我真的不喜欢当他们称它为关联的数组时......它只是搞砸了......它们不是数组:))。在您的情况下,您正在使用对象的属性。不同的是,该对象的属性没有订单。 (是的,它可能看起来像在某些浏览器中,其中属性的顺序是按时间顺序排列的)但通常,使用:for((obj中的索引)以RANDOM顺序返回属性。所以没有使用开始的空间。和结束,因为我们没有订单而我们没有整数索引。


您可能已经知道,我只是想为其他读者指出这一点。


So" jList"对象在两种情况下都是等价的:

展开 | 选择 | Wrap | 行号


嗨...


对象内的分号是不合法的...它会抛出一个错误:) ...再说一遍......


亲切的问候

In my ongoing effort to produce shorter, more efficient code, I have created a "chicken and egg" / "catch-22" problem. I can think of several ways to fix this, none of them elegant.

I want my code to declare var stop if it was not passed to the function. The problem is that stop would be equal to a value dependent on var index that has not been declared yet, but index cannot be created until stop is declared. So you see my chicken and egg problem.

At line 30 below, you can see that I declare stop when it is ''undefined''. However, index is not declared until line 42. So, how to handle this problem? And before you ask, no I haven''t run this code yet because I know it won''t work the way it is written. I''m not quite that dense. =D

Expand|Select|Wrap|Line Numbers

解决方案

heya ...

so have a look at the following code ... i made i working to test and simplified it as far as i could follow your explainations again :) ... at the moment the below code would produce:

Expand|Select|Wrap|Line Numbers


Just a side note: You have to make clear to yourself when you are working with "arrays" and when you are working with "object''s properties". In JS, array is created just when you are working with integer indexes. Otherwise it''s just object''s properties (I really dont like when they are calling it associative "arrays" .. it just mess things up .. they are not arrays :) ). In your case, you are working with object''s properties. The difference is, that object''s properties dont have an order. (yes, it may look like they have in some browsers, where order of properties are chronological) But in general, using: "for (index in obj)" returns properties in RANDOM order. So there is no room for using "start" and "end", because we have no order and we have no integer indexes.

You maybe already knew that, I just wanted to point that out for other readers.

So "jList" objects are equivalent in both cases:

Expand|Select|Wrap|Line Numbers


hi ...

the semicolon within the object is not legal ... it will throw an error :) ... just to mention it again ...

kind regards


这篇关于鸡肉和鸡肉通过JSON列表子集迭代的蛋问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆