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
  }
]
1

3 Answers 3

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

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

2 Comments

That's exactly what i need. Thanks a lot.
@DenisovJr. consider accepting answer, if it helped. Cheers!
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);

Comments

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)

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.