2

I have a following data:

const data2 = [
{
  App: "testa.com",
  Name: "TEST A",
  Category: "HR", 
  Employees: 7
},
{
  App: "testd.com",
  Name: "TEST D",
  Category: "DevOps", 
  Employees: 7
},
{
  App: "teste.com",
  Name: "TEST E",
  Category: "DevOps", 
  Employees: 7
},
{
  App: "testf.com",
  Name: "TEST F",
  Category: "Business", 
  Employees: 7
}
]

I want to get the count of distinct categories: Right now I am getting the list of all distinct categories but I'm unable to compute their count.

Following snippet give me the Distinct Category:

  let uniqueCategory = [];
  for(let i = 0; i < result.data.length; i++){    
      if(uniqueCategory.indexOf(result.data[i].Category) === -1){
        uniqueCategory.push(result.data[i].Category);        
      }        
  }

What changes should I make to get the Counts of those Categories in the uniqueCategory - something like following:

uniqueCategory = [
  {Category: "DevOps", count: 5},
  {Category: "Business", count: 4},
  ....
  {}
]
3
  • 1
    That's not a JSON object. It's a JS object. Commented Jul 23, 2020 at 20:03
  • when you say count, do you mean the sum of the Employees value? Commented Jul 23, 2020 at 20:03
  • @Anthony No, I mean the How many Time DevOps appears in the Object Commented Jul 23, 2020 at 20:12

3 Answers 3

2

Your approach implies looping your source array (with .indexOf()) every iteration of for(..-loop. That will slow down unnecessarily look up process.

Instead, you may employ Array.prototype.reduce() to traverse your source array and build up the Map, having Category as a key and object of desired format as a value, then extract Map.prototype.values() into resulting array.

That will perform much faster and scale better.

const src = [{App:"testa.com",Name:"TEST A",Category:"HR",Employees:7},{App:"testd.com",Name:"TEST D",Category:"DevOps",Employees:7},{App:"teste.com",Name:"TEST E",Category:"DevOps",Employees:7},{App:"testf.com",Name:"TEST F",Category:"Business",Employees:7}],

      result = [...src
        .reduce((r, {Category}) => {
          const cat = r.get(Category)
          cat ? cat.count ++ : r.set(Category, {Category, count: 1})
          return r
        }, new Map)
        .values()
      ]
    
console.log(result)
.as-console-wrapper{min-height:100%;}

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

Comments

1

The easiest way to do it is to use Array.prototype.reduce

const arr = [ ... ];
const output = arr.reduce((result, obj) => {
  if (!result[obj.category]) {
    result[obj.category] = 0;
  }

  result[obj.category]++;

  return result;
}, {});
console.log(output); // this should log the similar output you want

1 Comment

'...// this should log' you may post live snippet so that it will be in plain view whatever it logs
0

Here's another alternative using .map and Set:

const src = [
{
  App: "testa.com",
  Name: "TEST A",
  Category: "HR", 
  Employees: 7
},
{
  App: "testd.com",
  Name: "TEST D",
  Category: "DevOps", 
  Employees: 7
},
{
  App: "teste.com",
  Name: "TEST E",
  Category: "DevOps", 
  Employees: 7
},
{
  App: "testf.com",
  Name: "TEST F",
  Category: "Business", 
  Employees: 7
}
];

const categories = src.map(obj => obj.Category);
const distinctCategories = [...new Set(categories)];
console.log(distinctCategories.length);

1 Comment

I am also Looking for their counts to be pushed in distinctCategories as well. This gives me the distinct Category.

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.