计算嵌套js对象中项的长度

时间:2016-11-10 10:05:36

标签: javascript arrays

给定这样的数组,我如何获得特定类别中所有图表的计数。每个类别可以有多个组或没有组。

{
   "categories":[
      {
         "title":"category 1",
         "id":"cat1",
         "groups":[
            {
               "title":"group 1",
               "id":"grp1",
               "charts":[
                  {
                     "title":"chart 1",
                     "id":"chart1",
                     "type":"line"
                  }
               ]
            }
         ]
      },
      {
         "title":"category 2",
         "id":"cat2",
         "charts":[
            {
               "title":"chart 2",
               "id":"chart2",
               "type":"line"
            }
         ]
      },
      {
         "title":"category 3",
         "id":"cat3",
         "charts":[
            {
               "title":"chart 3",
               "id":"chart3",
               "type":"line"
            }
         ]
      }
   ]
}

4 个答案:

答案 0 :(得分:1)

单行可以吗?

假设数据是您的JSON结构:

data.categories
    .map(c => [
        c.title,
        c.groups ?
            c.groups.map(g => g.charts.length).reduce((a, b) => a+b) :
            c.charts.length
    ])

答案 1 :(得分:1)

var object = {
  "categories": [{
    "title": "category 1",
    "id": "cat1",
    "groups": [{
      "title": "group 1",
      "id": "grp1",
      "charts": [{
        "title": "chart 1",
        "id": "chart1",
        "type": "line"
      }]
    }]
  }, {
    "title": "category 2",
    "id": "cat2",
    "charts": [{
      "title": "chart 2",
      "id": "chart2",
      "type": "line"
    }]
  }, {
    "title": "category 3",
    "id": "cat3",
    "charts": [{
      "title": "chart 3",
      "id": "chart3",
      "type": "line"
    }]
  }]
}
var groupPerCategories = [];

object.categories.forEach(function(category) {
  var tot = 0;
  if (category.groups != undefined) {
    category.groups.forEach(function(group) {
      if(group.charts != undefined){
          tot += group.charts.length;
      }
    });
  }
  if (category.charts != undefined) {
    tot += category.charts.length;
  }
  console.log(tot);
});

答案 2 :(得分:0)

您可以使用默认值计算属性。



$ rostopic echo /status




答案 3 :(得分:0)

希望这会对你有所帮助:)。



var categories = [
      {
         "title":"category 1",
         "id":"cat1",
         "groups":[
            {
               "title":"group 1",
               "id":"grp1",
               "charts":[
                  {
                     "title":"chart 1",
                     "id":"chart1",
                     "type":"line"
                  }
               ]
            }
         ]
      },
      {
         "title":"category 2",
         "id":"cat2",
         "charts":[
            {
               "title":"chart 2",
               "id":"chart2",
               "type":"line"
            }
         ]
      },
      {
         "title":"category 3",
         "id":"cat3",
         "charts":[
            {
               "title":"chart 3",
               "id":"chart3",
               "type":"line"
            },
           {
               "title":"chart 3",
               "id":"chart3",
               "type":"line"
            },
           {
               "title":"chart 3",
               "id":"chart3",
               "type":"line"
            }
         ]
      },
    {
         "title":"category 4",
         "id":"cat4",
         "groups":[
            {
               "title":"group 4",
               "id":"grp4",
               "charts":[
                  {
                     "title":"chart 1",
                     "id":"chart1",
                     "type":"line"
                  },
                  {
                     "title":"chart 1",
                     "id":"chart1",
                     "type":"line"
                  }
               ]
            }
         ]
      }
   ];


function countCategoryItems(data, category) {
   if(!data ) return 0
   if(!category) return 0

   var cat = _.filter(data, function(obj){ return obj.title == category; });
   if(!cat.length) return 0

   var groups = cat[0].groups || []
   if(!groups.length) return cat[0].charts.length
   
   var count = 0
   for (var i=0;i<groups.length;i++) {
        count += groups[i].charts.length
   }
  
   return count
}

$(function() {
console.log(countCategoryItems(categories, 'category 1'))
console.log(countCategoryItems(categories, 'category 2'))
console.log(countCategoryItems(categories, 'category 3'))
console.log(countCategoryItems(categories, 'category 4'))
})
&#13;
<script src="http://underscorejs.org/underscore.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;