简体   繁体   English

递归地通过Json获得特定键的所有值

[英]Recursively pass through Json to get all values for a specific key

I have a json object which looks like this: 我有一个看起来像这样的json对象:

var testJ = {"ROOT":{
        dir : 'app',
        files : [
            'index.html',
            {
                dir : 'php',
                files: [
                    'a.php',
                    {
                        dir : 'extras',
                        files : [
                            'a.js',
                            'b.js'
                        ]
                    }
                ]
            }
        ]
    }};

I need to extract all the files and append into an array (index.html,a.php,a.js..etc) 我需要提取所有文件并追加到数组中(index.html,a.php,a.js..etc)

For this I wrote a javascript code as follows: 为此,我编写了一个JavaScript代码,如下所示:

var arr=[];
    function scan(obj,append)
    {
        var k;
        if (obj instanceof Object) {
            for (k in obj){
                if (obj.hasOwnProperty(k)){
                    if(k=='files')
                    {
                      scan( obj[k],1 );  
                    }


                }                
        }
    } else {
        body += 'found value : ' + obj + '<br/>';
        if(append == 1)
        arr.push(obj);

        alert("Arr"+ arr);
    };

};

scan(testJ,0);

I am not able to figure out where am I going wrong. 我无法弄清楚我要去哪里。 Could some give me pointers? 可以给我一些指示吗?

var res = [];
function gather(j) {
  for (var k in j) {
    if (k === 'files') {
      addFiles(j[k]);
    } else if (typeof j[k] === 'object') {
      gather(j[k]);
    }
  }
}
function addFiles(f) {
  for (var i = 0; i < f.length; i++) {
    if (typeof f[i] === "string") {
      body += 'found value : ' + obj + '<br/>';
      res.push(f[i]);
    } else {
      gather(f[i]);
    }
  }
}
gather(testJ);

Free tips: 免费提示:

instanceof is some cancerous stuff. instanceof是一些有毒的东西。 Why does instanceof return false for some literals? 为什么instanceof对某些文字返回false?

Always, always use === for comparison, not == 始终,总是使用===进行比较,而不是==

I also wouldn't blindly use hasOwnProperty unless you're afraid the thing you're operating on might have a modified prototype, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in just for simplicity. 我也不会盲目使用hasOwnProperty除非您担心所操作的东西可能具有经过修改的原型, https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/为了...只是为了简单。

How about a map reduce approach?: 地图缩小方法怎么样?:

function mapJ(subject) {
  return subject.files.map(function(item) {
    if (typeof item === "string") {
      return item;
    } else {
      return parseJ(item);
    }
  });
}

function reduceJ(subject) {
  return subject.reduce(function(prev, cur) {
    return prev.concat(cur);
  }, []);
}

function parseJ(subject) {
  return reduceJ(mapJ(subject));
}

var result = parseJ(testJ));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM