【问题标题】:How to read recursively an object which looks like that如何递归读取看起来像这样的对象
【发布时间】:2012-08-27 11:25:47
【问题描述】:

我有以下对象:

var xhr = JSON.parse('{"name1":{"errors":["This value should not be blank."]}, "children":{"name2":{"errors":["This value should not be blank."]},"message":[],"name3":{"errors":["This value should not be blank."]}, "children":{"name4":{"errors":["This value should not be blank."]} }}}');

console.log(xhr);

我需要递归读取xhr 对象。
我发布的对象只是一个例子,这意味着孩子可能或多或少。
Anywas objectReader 应该能够得到以下输出:

name1 ["This value should not be blank."] 
name2 ["This value should not be blank."] 
name3 ["This value should not be blank."] 
name4 ["This value should not be blank."] 

我确实尝试编写以下部分工作的代码:

_.each(xhr, function (xhrObject, name) {
    if(xhrObject.errors) {
        console.log(name, xhrObject.errors);
    }
});

这是http://jsfiddle.net/UWEMT/ 资源。
使用下划线如何完成此任务的任何想法?谢谢。 ​

【问题讨论】:

  • 这个JSON很奇怪,看看能不能改进一下生成器

标签: javascript object underscore.js


【解决方案1】:

看到这个:http://jsfiddle.net/UWEMT/6/

prs(xhr);

function prs(x){
        _.each(x, function (xhrObject, name) {
            if(xhrObject.errors) {
                console.log(name, xhrObject.errors);
            }
            else prs(xhrObject);
        })
}
​

如果对象有错误,则为结束节点,否则为包含更多对象的子节点。

【讨论】:

    【解决方案2】:

    你那里有一些看起来很奇怪的 json...

    但是你可以像这样进行递归循环:

    var xhr = JSON.parse('{"name1":{"errors":["This value should not be blank."]}, "children":{"name2":{"errors":["This value should not be blank."]},"message":[],"name3":{"errors":["This value should not be blank."]}, "children":{"name4":{"errors":["This value should not be blank."]} }}}');
    
    function loop( json ) {
        _.each(json, function (value, key) {
            if(value.errors) {
                console.log(key, value.errors);
            }
            else {
                 loop(value);     
            }
        });
    }
    
    loop(xhr);​
    

    http://jsfiddle.net/YE6Qn/1/

    【讨论】:

      【解决方案3】:
      var xhr = JSON.parse("…");
      
      (function recurse(obj) {
          for (var name in obj) {
              if (name != "children")
                  console.log(name, obj[name].errors);
          }
          if ("children" in obj)
              recurse(obj.children);
      })(xhr);
      

      这段代码反映了你的 JSON 的奇怪结构(例如,你的名字不能是“孩子”)

      【讨论】:

      【解决方案4】:

      一个简单的 js 解决方案:

      function convertObj(obj) {     
        for (var p in obj) {
          if (obj.hasOwnProperty(p)) {
            if (obj[p].errors) {
              console.log(p, obj[p].errors);
            } else {
              convertObj(obj[p]);
            }
          }
        }
      }
      

      【讨论】:

      • 为什么是hasOwnProperty?输入是从 JSON 解析的,是一个普通对象
      • 我认为需要一个通用解决方案,因此请注意 Object.prototype 具有可枚举属性的情况(人们会做一些愚蠢的事情)。如果 OP 需要,可以省略它,我不会。
      【解决方案5】:

      只是用户递归,jsfidle - http://jsfiddle.net/UWEMT/7/

      _.each(xhr, function read(item, name) {
          if(name == "children") {
              _.each(item, read);
          }
      
          if (item.errors) {
              console.log(name, item.errors)
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-24
        • 2012-03-25
        • 2020-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多