mootools javascript return Array.each() 带递归
mootools javascript return Array.each() with recursion
我有一个 JSON 对象,我想要实现的是,我可以通过 ID 在对象中搜索 child_objects。我正在使用 Array.each()
和递归函数,如下所示:
1 Template.get_object_attributes_by_id = function(id, template)
2 {
3 var template_obj = JSON.parse(template);
4 console.log(Template.check_for_id_equality(template_obj, id);
5 return Template.check_for_id_equality(template_obj, id);
6 }
7
8 Template.check_for_id_equality = function(obj, id)
9 {
10 if (obj.attrs.id !== id) {
11 if (obj.children === null || obj.children === undefined) {
12 return;
13 }
14 Array.each(obj.children, function(obj_child) {
15 return Template.check_for_id_equality(obj_child, id);
16 });
17 }
18 else {
19 console.log(obj);
20 return obj;
21 }
22 }
调用Template.get_object_attributes_by_id(id, template)
后第19行的输出是正确的对象,但是第4行的输出是undefined
。
Array.each()
"ignores" return 似乎一直在继续。所以现在我的问题是,如何正确 return 对象,以便我将在函数 get_object_attributes_by_id()
.
中获取它
更新:
输入(模板)是一个如下所示的 JSON 对象,例如我在其中搜索 ID "placeholder-2"。这只是一个例子,所以请不要寻找缺少的括号或类似的东西,因为我使用的真实 JSON 对象显然是有效的。
{
"className":"Stage",
"attrs":{
"width":1337,
"height":4711
},
"children":[{
"className":"Layer",
"id":"placeholder-layer",
"attrs":{
"width":1337,
"height": 4711
},
"children":[{
"className":"Text",
"id":"placeholder-1",
"attrs":{
"fontsize":42,
"color":"black",
...
}
},
{
"className":"Text",
"id":"placeholder-2",
"attrs":{
"fontsize":37,
"color":"red",
...
}
},
...
]
}]
}
这就是预期的输出:
{
"className":"Text",
"id":"placeholder-2",
"attrs":{
"fontsize":37,
"color":"red",
...
},
}
经过更多的调查和尝试,我和我的同事通过使用 "for" 循环而不是 "Array.each()" 自己解决了这个问题。
解决方法如下:
1 Template.get_object_attributes_by_id = function(id, template)
2 {
3 var template_obj = JSON.parse(template);
4 console.log(Template.check_for_id_equality(template_obj, id);
5 return Template.check_for_id_equality(template_obj, id);
6 }
7
8 Template.check_for_id_equality = function(obj, id)
9 {
10 if (obj.attrs.id === id) {
11 return obj;
12 }
13 if (obj.children === null || obj.children === undefined) {
14 return false;
15 }
16 for (var i = 0; i < obj.children.length; i++) {
17 var ret_val = Template.check_for_id_equality(obj.children[i], id);
18 if (ret_val !== false) {
19 return ret_val;
20 }
21 }
22 return false;
23 }
我有一个 JSON 对象,我想要实现的是,我可以通过 ID 在对象中搜索 child_objects。我正在使用 Array.each()
和递归函数,如下所示:
1 Template.get_object_attributes_by_id = function(id, template)
2 {
3 var template_obj = JSON.parse(template);
4 console.log(Template.check_for_id_equality(template_obj, id);
5 return Template.check_for_id_equality(template_obj, id);
6 }
7
8 Template.check_for_id_equality = function(obj, id)
9 {
10 if (obj.attrs.id !== id) {
11 if (obj.children === null || obj.children === undefined) {
12 return;
13 }
14 Array.each(obj.children, function(obj_child) {
15 return Template.check_for_id_equality(obj_child, id);
16 });
17 }
18 else {
19 console.log(obj);
20 return obj;
21 }
22 }
调用Template.get_object_attributes_by_id(id, template)
后第19行的输出是正确的对象,但是第4行的输出是undefined
。
Array.each()
"ignores" return 似乎一直在继续。所以现在我的问题是,如何正确 return 对象,以便我将在函数 get_object_attributes_by_id()
.
更新:
输入(模板)是一个如下所示的 JSON 对象,例如我在其中搜索 ID "placeholder-2"。这只是一个例子,所以请不要寻找缺少的括号或类似的东西,因为我使用的真实 JSON 对象显然是有效的。
{
"className":"Stage",
"attrs":{
"width":1337,
"height":4711
},
"children":[{
"className":"Layer",
"id":"placeholder-layer",
"attrs":{
"width":1337,
"height": 4711
},
"children":[{
"className":"Text",
"id":"placeholder-1",
"attrs":{
"fontsize":42,
"color":"black",
...
}
},
{
"className":"Text",
"id":"placeholder-2",
"attrs":{
"fontsize":37,
"color":"red",
...
}
},
...
]
}]
}
这就是预期的输出:
{
"className":"Text",
"id":"placeholder-2",
"attrs":{
"fontsize":37,
"color":"red",
...
},
}
经过更多的调查和尝试,我和我的同事通过使用 "for" 循环而不是 "Array.each()" 自己解决了这个问题。
解决方法如下:
1 Template.get_object_attributes_by_id = function(id, template)
2 {
3 var template_obj = JSON.parse(template);
4 console.log(Template.check_for_id_equality(template_obj, id);
5 return Template.check_for_id_equality(template_obj, id);
6 }
7
8 Template.check_for_id_equality = function(obj, id)
9 {
10 if (obj.attrs.id === id) {
11 return obj;
12 }
13 if (obj.children === null || obj.children === undefined) {
14 return false;
15 }
16 for (var i = 0; i < obj.children.length; i++) {
17 var ret_val = Template.check_for_id_equality(obj.children[i], id);
18 if (ret_val !== false) {
19 return ret_val;
20 }
21 }
22 return false;
23 }