简体   繁体   中英

Generate aggregate array of JSON object in angular

Here is my JSON object

    $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"
    }]
};

Using this i am trying to generate below info

  1. Number of Items in each category for which I have generated an array series = ['Veg','Non-veg'] with the below code:
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;

I am trying to generate another arry SeriesCount for which expected values are

SeriesCount = [4,1] 

somehow not yet reached the resul, can somebody please help me here.

  1. Want to generate cost bucket and respective count of items.. Expected

      CostBucketSeries = ['0-100' , '101-300' , '> 300' ]; CostBucketSeriesCount =[3,2,0]; 

Using angular-chart i want to show this... Thanks in advance..

Q: I am trying to generate another arry SeriesCount for which expected values are.

A: There are many ways you can get the series count. Eg iterating data1.data and have an object to track the counts for each type of item.

In my example below, I am using a combination of the map and forEach API to solve the count problem. The solution is an elegant one as it does not require you to know what types of item you have upfront.

Note: You might want to jump straight to the solution. see // solution comment in code.

Sample code :

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

Output:

[ 4, 1 ]

What I got there is I first make use of what you got in your series data by going through the individual type in there and loop through the entities in data1.data , if they matches the type then increment. By the end of map - you will get an array comprising of the counts.

Note: Performance is not taken into account for this solution as you might noticed that it has a time complexity of O(n2) .

One possible way of improving is to replace the forEach call with filter instead. This is so that you don't recount items that has already been processed before.

Reference for the APIs:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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