迭代部分JSON属性列表 [英] Iterate through partial JSON property list

查看:104
本文介绍了迭代部分JSON属性列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像往常一样,一个过于漫长,过于解释的帖子。更好的信息而不是太少,对吧?


几周前,我要求一些帮助迭代JSON属性列表,以便我的代码可以选择下一个值成员列表或单个值。原帖可以在这里找到。


这是代码gits帮我写的:

展开 | 选择 | Wrap | 行号

解决方案

以前,我询问如何遍历JSON属性列表。 gits帮助大大提高了,我有一个工作脚本。现在我想进一步修改它。


我想迭代一部分属性列表。目前,我已经设法通过合并一个''split''(或splice)变量来实现这一目标,该变量定义了从哪里开始迭代。当我输入它时,我意识到我可以通过合并一个''end split''变量来添加更多功能。但是我稍后会解决这个问题。


这就是问题所在。当我遍历整个列表(没有拆分)时,它运行良好。当我使用拆分时,属性列表中的第一个值按设计工作,即单独使用,而不是迭代。但是,拆分的迭代部分仅显示最后一个值。我怀疑正在发生的是每个值都在显示,但是下一个值会删除前一个值。我怀疑这是因为迭代开始时显示的非迭代值会消失,这就是我想要的。对于我的生活,我无法弄清楚为什么其他价值观会消失。 =(


现在万一没有任何意义,这里是我的代码,带有符号来解释。

展开 | 选择 | Wrap | 行号


嗨...


就在第一次快照中看到的......你不能像以前那样简单地使用比较运算符......看看下面的例子:

展开 | 选择 | 换行 | 行号


你不能像以前那样简单地使用比较运算符 ...你必须循环通过obj,当找到你的''split''键时,你必须使用该键和所有后续键及其值构建一个新的obj ...然后使用该列表进行进一步处理...这意味着, 你必须将列表分成两个列表。



GASP!我的天啊!!说它不是这样,gits!我其实......< GULP!> ...... dis ... ... 与你不一致(这很难说!)......不是一次,但两次甚至!!我心爱的脚本导师的形象永远玷污了。无论我做什么!? = D


好​​的,好吧,也许我第一次没有解释得很好,但在我努力澄清的时候,我可能已经用测试解决了问题下面的代码 - 只需将其应用于createDOM()并进行测试。


比较运算符可能无法比较您的想法。你只是快速看一下;所以这完全可以理解。 ''i''和''split''都是通过输入loadLogin()函数和静态数确定的数值;它们不是JSON列表的一部分。


如果您注意到原始帖子中属性列表示例的末尾,则会出现i和分裂在之后列出的 JSON列表已关闭。基本上,它将三个独立的对象传递给createDOM():''attribList'',''split''和''i''。从现在开始,我将JSON属性列表设置为变量,然后将var传递给createDOM()。这应该不那么容易混淆了。


这是我的测试代码,适用于IE,FF和NN:

展开 | 选择 | Wrap | 行号

As usual, an overly-long, overly-explanatory post. Better too much info than too little, right?

A couple weeks ago, I asked for some assistance iterating through a JSON property list so that my code would either select the next value in the member list or the single value. The original post can be found here.

This is the code gits helped me write:

Expand|Select|Wrap|Line Numbers

解决方案

Previously, I asked how to iterate through a JSON property list. gits helped out considerably with that, and I have a working script. Now I want to modify it further.

I want to iterate through only part of the property list. Currently, I have managed to accomplish this partially by incorporating a ''split'' (or splice) variable, which defines where to begin iterating from. As I type this, I just realized that I can add further functionality by incorporating an ''end split'' variable as well. But I will address that later.

Here''s the problem. When I am iterating through the entire list (no split), it works great. When I use the split, the first values in the property list work as designed, that is individually, not iterated through. However, the iteration portion of the split only displays the very last value. I suspect what is happening is that each value is being displayed, but the next value removes the previous one. I suspect this because the non-iterated values that display first disappear when the iteration begins, which is what I want. For the life of me, I can''t figure out why the other values are going away. =(

Now in case none of that makes sense, here is my code with notation to explain.

Expand|Select|Wrap|Line Numbers


hi ...

as far as could see at a first quickshot ... you cannot simply use the compare operator as you did ... have a look at the following example:

Expand|Select|Wrap|Line Numbers


you cannot simply use the compare operator as you did ... you have to loop through the obj and when finding your ''split'' key you have to build a new obj with that and all following keys and their values ... and then use that list for further processing ... which means, you have to split the list physically into two lists.

GASP! OMG!! Say it ain''t so, gits! I actually ... <GULP!> ... dis- ... disa- ... disagree with you (that was so hard to say!) ... not once, but twice even!! My image of my beloved scripting mentor has been tarnished forever. Whatever shall I do!? =D

Ok, ok, so maybe I didn''t explain it very well the first time around, but in my effort to clarify, I may have resolved the problem with the test code below--just need to apply it to createDOM() and test.

The compare operator may not be comparing what you think it is. You only took a quick look; so that is totally understandable. ''i'' and ''split'' are both numerical values determined by input into the loadLogin() function and a static number respectively; they are not part of the JSON list.

If you notice at the end of the property list example in the original post, there is an ''i'' and a ''split'' listed after the JSON list is closed. So essentially, it is passing three separate objects to createDOM(): ''attribList'', ''split'', and ''i''. From now on, I will set the JSON property list to a variable, and then pass the var to createDOM(). That should be less confusing.

Here is my test code, which works in IE, FF, and NN:

Expand|Select|Wrap|Line Numbers


这篇关于迭代部分JSON属性列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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