Javascript:使用遍历从 JSON 对象获取引用
我需要从 JSON 对象获取引用,代码如下:
var Tree = { data: { 0: { pk: 1, }, 1: { pk: 2, }, 2: { pk: 3, children: { 0: { pk: 11, }, 1: { pk: 22, }, 2: { pk: 33, }, }, }, }, traverse: function(data, pk) { for (i in data) { // console.log(data[i]); if(data[i].pk && data[i].pk == pk) return data[i]; if (typeof(data[i].children) == 'object') this.traverse(data[i].children, pk); }; }, }
遍历顶级项目时,代码运行得很好:
>>> Tree.traverse(Tree.data, 1); Object {pk=1}
但是在获取子元素时就损坏了:
>>> Tree.traverse(Tree.data, 22); undefined
当您取消注释 ' 时,我很奇怪为什么会出现这种行为// console.log(data[i]);'行您将看到对象已获取但未返回。
有什么想法吗?
I need to get a reference from JSON object, the code just following:
var Tree = { data: { 0: { pk: 1, }, 1: { pk: 2, }, 2: { pk: 3, children: { 0: { pk: 11, }, 1: { pk: 22, }, 2: { pk: 33, }, }, }, }, traverse: function(data, pk) { for (i in data) { // console.log(data[i]); if(data[i].pk && data[i].pk == pk) return data[i]; if (typeof(data[i].children) == 'object') this.traverse(data[i].children, pk); }; }, }
The code works very well when traverse the top level items:
>>> Tree.traverse(Tree.data, 1); Object {pk=1}
But broken when get the child element:
>>> Tree.traverse(Tree.data, 22); undefined
I'm very strange why it's this behavior, when you uncomment the '// console.log(data[i]);' line you will see the object is got but didn't returned.
Any idea about it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有将
return
放在this.traverse(data[i].children, pk);
之前。编辑:
在这里检查实时:http://jsfiddle.net/rq4LK/
You didn't put
return
beforethis.traverse(data[i].children, pk);
.EDIT:
check live here: http://jsfiddle.net/rq4LK/
这是一个不太可能的事情,但您正在
for
循环中创建一个全局变量。尝试使用for(var i in data)
来代替,然后请报告。如果这不是整个对象,并且您在父
Object
文本中有一个带有键的属性(如3: ...
),但Object
文本中不存在该属性, code>children 子属性,它显然会返回undefined
,因为该键没有这样的属性。编辑:根据您的评论,这也可能是函数作用域的问题,因为您使用尾递归来迭代具有多层的对象。
因此,尝试将当前对象引用置于函数作用域之外,就像在任何需要动态引用的 javascript 语言构造中所做的那样:
编辑 2: 我的逻辑有缺陷。请尝试这样做:
使用该方法,如果您的对象包含指定的属性,
match
将不会为null
并且将包含匹配的对象或子对象。This is a long shot, but you are creating a global variable in your
for
loop. Tryfor(var i in data)
instead and then report back please.In case this is not the whole object and you have a property with a key (like
3: ...
) in the parentObject
literal that does not exist in thechildren
child property, it will obviously returnundefined
, as there is no such property by that key.Edit: As per your comment, this might also be a problem with function scope as you are using tail recursion to iterate over an object with multiple layers.
Thus, try to put the current object reference outside the function scope as you would do in any javascript language construct that requires a dynamic reference:
Edit 2: Flawed logic on my part. Try this instead:
Using that, if your object contains the specified property
match
won't benull
and will contain the matching object or child object.