Javascript递归重置计数

时间:2016-02-15 10:44:13

标签: javascript json

我正在尝试递归计算JSON对象中的“file”数。

这是我的功能,

function totalReports(object, count) { 
    count = count || 0;

    var key;
    for(key in object) {
        if(typeof object[key] == 'object') {
          if(object[key].type == 'file') {
             count += 1;
             console.log(count);            
          }
          console.log("count");            
          totalReports(object[key], count);
        }
    }
    return count;
}

这是JSON对象,

jsonArray = {  
   "parent":{  
      "url":"parent.html",
      "name":"Parent",
      "children":[  
         {  
            "type":"folder",
            "name":"Folder 1",
            "url":"folder1.html",
            "children":[  
               {  
                  "type":"file",
                  "name":"File 1",
                  "url":"folder1-file1.html",
                  "children":[  

                  ]
               },
               {  
                  "type":"file",
                  "name":"File 2",
                  "url":"folder1-file2.html",
                  "children":[  

                  ]
               },
               {  
                  "type":"file",
                  "name":"File 2",
                  "url":"folder1-file3.html",
                  "children":[  

                  ]
               }
            ]
         },
         {  
            "type":"folder",
            "name":"Folder 2",
            "url":"folder2.html",
            "children":[  

            ]
         },
         {  
            "type":"folder",
            "name":"Folder 3",
            "url":"folder3.html",
            "children":[  
               {  
                  "type":"file",
                  "name":"Folder3 File1",
                  "url":"folder3-file1",
                  "children":[  

                  ]
               },
               {  
                  "type":"folder",
                  "name":"Folder3 Folder1",
                  "url":"folder3-file1",
                  "children":[  
                     {  
                        "type":"folder",
                        "name":"Folder3 Folder1 Folder1",
                        "url":"folder3-folder1-folder1",
                        "children":[  
                           {  
                              "type":"file",
                              "name":"Folder3 Folder1 Folder1 File1",
                              "url":"folder3-folder1-folder1-file1",
                              "children":[  

                              ]
                           },
                           {  
                              "type":"file",
                              "name":"Folder3 Folder1 Folder1 File2",
                              "url":"folder3-folder1-folder1-file2",
                              "children":[  

                              ]
                           }
                        ]
                     }
                  ]
               }
            ]
         }
      ]
   }
};

它被称为console.log(totalReports(jsonArray))。它正在进入if循环正确的次数,但是当它到达下一个子数组时它没有保持该值。这里的期望值是6,它进入if循环6次,但不知道为什么它重置值,当它出来时。

这是相同的小提琴,https://jsbin.com/kogoko/edit?js,console,output

任何帮助都会很棒。提前谢谢。

2 个答案:

答案 0 :(得分:3)

Count是一个数字,它是按值传递的,而不是通过JavaScript中的引用传递的。你需要做的就是改变这一行。

totalReports(object[key], count);

count= totalReports(object[key], count);

答案 1 :(得分:3)

如果您出于不同目的不需要count,我建议您删除参数count并返回区域设置count



function totalReports(object) {
    var count = 0;
    Object.keys(object).forEach(function (key) {
        if (object[key] && typeof object[key] === 'object') {
            if (object[key].type === 'file') {
                count++;
            }
            count += totalReports(object[key]);
        }
    });
    return count;
}

var jsonArray = { "parent": { "url": "parent.html", "name": "Parent", "children": [{ "type": "folder", "name": "Folder 1", "url": "folder1.html", "children": [{ "type": "file", "name": "File 1", "url": "folder1-file1.html", "children": [] }, { "type": "file", "name": "File 2", "url": "folder1-file2.html", "children": [] }, { "type": "file", "name": "File 2", "url": "folder1-file3.html", "children": [] }] }, { "type": "folder", "name": "Folder 2", "url": "folder2.html", "children": [] }, { "type": "folder", "name": "Folder 3", "url": "folder3.html", "children": [{ "type": "file", "name": "Folder3 File1", "url": "folder3-file1", "children": [] }, { "type": "folder", "name": "Folder3 Folder1", "url": "folder3-file1", "children": [{ "type": "folder", "name": "Folder3 Folder1 Folder1", "url": "folder3-folder1-folder1", "children": [{ "type": "file", "name": "Folder3 Folder1 Folder1 File1", "url": "folder3-folder1-folder1-file1", "children": [] }, { "type": "file", "name": "Folder3 Folder1 Folder1 File2", "url": "folder3-folder1-folder1-file2", "children": [] }] }] }] }] } },
    count = totalReports(jsonArray);

document.write(count);