以角度生成JSON对象的聚合数组

时间:2016-07-11 03:54:30

标签: javascript angularjs json

这是我的JSON对象

    $scope.data1 = {
    "totalSize": 5,
    "data": [{
        "id": "AGGAA5V0HGQXEAOJJQitemWOI41FJLY2S",
        "price": 100.00,
        "desc": "Onion and Tomato toppings Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima molestiae cum optio praesentium doloribus, inventore nobis nostrum sequi quidem corporis iure ut natus nemo maxime vitae assumenda aliquam blanditiis. Alias!",
        "status": true,
        "item": "Piza",
        "type": "Veg"
    }, {
        "id": "AGGAA5V0HGQXEAOJJQ9HOI41F9LY2T",
        "price": 90.00,
        "desc": null,
        "status": 0,
        "item": "Pasta",
        "type": "Veg"
    }, {
        "id": "AGGAA5V0HGQXEAOJJQKBOI41G3LY2U",
        "price": 150.00,
        "desc": null,
        "status": 0,
        "item": "Italian Piza",
        "type": "Veg"
    }, {
        "id": "AGGAA5V0HGQXEAOJJS5ZOI43C1LY2V",
        "price": 300.00,
        "desc": null,
        "status": 0,
        "item": "Aloo Paratha",
        "type": "Non-Veg"
    }, {
        "id": "AGGAA5V0HGQXEAOJJSZHOI43L9LY2W",
        "price": 50.00,
        "desc": null,
        "status": 0,
        "item": "Maggie",
        "type": "Veg"
    }]
};

使用此我试图生成以下信息

  1. 我使用以下代码生成数组series = ['Veg','Non-veg']的每个类别中的项目数:
  2. var series = ($scope.data1.data).reduce(function(res, obj) {
        if (!(obj.type in res))
            res.__array.push(res[obj.type] = obj.type);
        else {
        }
        return res;
    }, {__array:[]}).__array;
    

    我正在尝试生成另一个预期值为

    的arry SeriesCount
    SeriesCount = [4,1] 
    

    某种程度上尚未达到结果,有人可以帮助我。

    1. 想要生成成本桶和各自的项目数量。 预计

       CostBucketSeries = ['0-100' , '101-300' , '> 300' ];
       CostBucketSeriesCount =[3,2,0];
      
    2. 使用角度图我想显示这个...... 提前谢谢..

1 个答案:

答案 0 :(得分:3)

问:我正在尝试生成另一个预期值为的arry SeriesCount。

答:您可以通过多种方式获取系列计数。例如。迭代data1.data并有一个对象来跟踪每种类型项目的计数。

在下面的示例中,我使用mapforEach API的组合来解决计数问题。该解决方案是一个优雅的解决方案,因为它不需要您了解您预先拥有的项目类型。

注意:您可能希望直接跳转到解决方案。请参阅代码中的// solution条评论。

示例代码

var data1 = {
  "totalSize": 5,
  "data": [{
    "id": "AGGAA5V0HGQXEAOJJQitemWOI41FJLY2S",
    "price": 100.00,
    "desc": "Onion and Tomato toppings Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima molestiae cum optio praesentium doloribus, inventore nobis nostrum sequi quidem corporis iure ut natus nemo maxime vitae assumenda aliquam blanditiis. Alias!",
    "status": true,
    "item": "Piza",
    "type": "Veg"
  }, {
    "id": "AGGAA5V0HGQXEAOJJQ9HOI41F9LY2T",
    "price": 90.00,
    "desc": null,
    "status": 0,
    "item": "Pasta",
    "type": "Veg"
  }, {
    "id": "AGGAA5V0HGQXEAOJJQKBOI41G3LY2U",
    "price": 150.00,
    "desc": null,
    "status": 0,
    "item": "Italian Piza",
    "type": "Veg"
  }, {
    "id": "AGGAA5V0HGQXEAOJJS5ZOI43C1LY2V",
    "price": 300.00,
    "desc": null,
    "status": 0,
    "item": "Aloo Paratha",
    "type": "Non-Veg"
  }, {
    "id": "AGGAA5V0HGQXEAOJJSZHOI43L9LY2W",
    "price": 50.00,
    "desc": null,
    "status": 0,
    "item": "Maggie",
    "type": "Veg"
  }]
};

var series = (data1.data).reduce(function(res, obj) {
  if (!(obj.type in res))
    res.__array.push(res[obj.type] = obj.type);
  else {
  }
  return res;
}, {__array:[]}).__array;

// Solution:
var seriesChart = series.map(type => {
  var count = 0;
  data1.data.forEach( item => {
    if(item.type === type) {
      count += 1;
    }
  });
  return count;
});

<强>输出:

  

[4,1]

我得到的是我首先通过浏览series中的个人类型来使用你在data1.data数据中获得的内容并循环遍历map中的实体,如果它们匹配类型那么增量。到forEach结束时,您将获得一个包含计数的数组。

注意:此解决方案未考虑性能,因为您可能会注意到它的时间复杂度为 O(n2)

一种可行的改进方法是将filter来电替换为private boolean isCompletelyWritten(File file) throws InterruptedException{ Long fileSizeBefore = file.length(); Thread.sleep(3000); Long fileSizeAfter = file.length(); System.out.println("comparing file size " + fileSizeBefore + " with " + fileSizeAfter); if (fileSizeBefore.equals(fileSizeAfter)) { return true; } return false; } 。这样您就不会重新计算之前已处理过的项目。

API参考:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach