4

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

4
  • No I just want to get their count Commented Jul 11, 2016 at 3:57
  • e.g. Series = ['Veg','Non-veg'] , SeriesCount = [4,1] .. this ill be sending to angular charts Commented Jul 11, 2016 at 3:58
  • please mention the question clearly you are adding that in comment section please edit your question Commented Jul 11, 2016 at 3:58
  • Yes its there i guess Commented Jul 11, 2016 at 3:59

1 Answer 1

3

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. E.g. 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.