1

I have an array:

[
  {
    theme: "low battery",
    Number: "S-10",
    components: [ "hardware", "battery" ]
  },
  {
    theme: "bad sync",
    Number: "S-11",
    components: [ "software" ]
  },
  {
    theme: "misc troubles",
    Number: "S-12",
    components: [ "hardware", "software" ]
  }
]

So i want to group this array by "components" values. In other words, i want to know, how many times does component occures in array. For example:

[
  {
    component: "hardware",
    amount: 2
  },
  {
    component: "software",
    amount: 2
  },
  {    
    component: "battery",
    amount: 1
  }
]
Denisov Jr.
  • 91
  • 3
  • 11
  • 1
    Possible duplicate of [What is the most efficient method to groupby on a javascript array of objects?](https://stackoverflow.com/questions/14446511/what-is-the-most-efficient-method-to-groupby-on-a-javascript-array-of-objects) – Andreas Jul 07 '17 at 08:13

3 Answers3

1

You can create a map to store the count of each component in an efficient way (lookup complexity of O(1)):

var items = [
  {
    theme: "low battery",
    Number: "S-10",
    components: [ "hardware", "battery" ]
  },
  {
    theme: "bad sync",
    Number: "S-11",
    components: [ "software" ]
  },
  {
    theme: "misc troubles",
    Number: "S-12",
    components: [ "hardware", "software" ]
  }
];

var componentMap = new Map();

items.forEach((item) => {
  item.components.forEach((component) => {
    if (componentMap.has(component)) {
      componentMap.set(component, componentMap.get(component) + 1);
    }
    else {
      componentMap.set(component, 1);
    }
  });
});

var componentCount = [];

componentMap.forEach((value, key) => {
  componentCount.push({
    component: key,
    amount: value
  });
});

console.log(componentCount);
Alberto Trindade Tavares
  • 10,056
  • 5
  • 38
  • 46
1

   

 var arr = [
      {
        theme: "low battery",
        Number: "S-10",
        components: [ "hardware", "battery" ]
      },
      {
        theme: "bad sync",
        Number: "S-11",
        components: [ "software" ]
      },
      {
        theme: "misc troubles",
        Number: "S-12",
        components: [ "hardware", "software" ]
      }
    ];
    var components = {};
    arr.forEach(function(val){
        val.components.forEach(function(c){
           components[c] = !(c in components) ? 1 : Number(components[c])+1;
        });
    });

    var componentCount = [];
    for(key in components){
     componentCount.push({'component' : key, 'amount' : components[key]});
    }
    
   console.log(componentCount);
Dij
  • 9,761
  • 4
  • 18
  • 35
1

You can use forEach() loop and thisArg parameter to store each component and increment amount.

var data = [{"theme":"low battery","Number":"S-10","components":["hardware","battery"]},{"theme":"bad sync","Number":"S-11","components":["software"]},{"theme":"misc troubles","Number":"S-12","components":["hardware","software"]}]

var result = []

data.forEach(function(e) {
  var that = this;
  e.components.forEach(function(c) {
    if(!that[c]) {
      that[c] = {component: c, amount: 0}
      result.push(that[c])
    }
    that[c].amount += 1
  })
}, {})

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176