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

Count length of items in nested js object

本文关键字:项目 嵌套 js 对象 计算      更新时间:2023-09-26

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

{
   "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"
            }
         ]
      }
   ]
}

一行行可以吗?

假设数据是JSON结构:

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

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);
});

您可以计算默认属性。

var data = { "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" }] }] },
    count = {};
data.categories.forEach(function (a) {
    var countCharts = function (r, a) {
        return r + (a.charts || []).length;
    };
    count[a.title] = (count[a.title] || 0) + 
        (a.groups ||[]).reduce(countCharts, 0) +
        countCharts(0, a);
});
console.log(count);

希望这对你有帮助:)

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'))
})
<script src="http://underscorejs.org/underscore.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>