Generate a cumulative array of a 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 create below info

  • The number of elements in each category for which I created an array series = ['Veg','Non-veg']with the code below:
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 create another arry SeriesCount for which the expected values

SeriesCount = [4,1] 

somehow still not achieved the result, can someone please help me here.

  1. Want to create a cost calculator and an appropriate number of elements. Expected

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

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

+4
source share
1 answer

Q: I'm trying to create another arry SeriesCount for which the expected values.

A: . . data1.data .

API map forEach . , , , .

. . . // 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 - , .

. , , O (n2).

forEach filter. , , .

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

+3

All Articles