0

How can I group my data by CustomerID?

It's does not work, I am trying different way but I think I don't understand this structure

This is my code:

function CustomerGroup() {
this.$http.post(auth.API_URL + 'api/ProblemsSupports/ProblemsList', null, {
    headers: {
        'Authorization': 'Bearer ' + auth.getAuthHeader()
    }
}).then((response) => {
    this.problemitems = response.body
    const map = this.problemitems.map(e => (map[e.CustomerID] = map[e.CustomerID]))
    return map
    this.progress = false
}, (response) => {
    if (response.status === 0) {
        this.text1 = ' !!!'
        this.snackbar = true
        this.progress = false
    } else {
        this.text1 = '!!!!!!'
        this.snackbar = true
        this.progress = false
    }
})
}
muya.dev
  • 966
  • 1
  • 13
  • 34
  • what are you trying to achieve ? what is actually happening ? – Towkir Nov 24 '19 at 15:46
  • I have diffrent problems value. I need to group this problems for customerID , for example - X COMPANY - 40 Problems - Y COMPANY - 50 Problems – Furkan Yılmaz Nov 24 '19 at 17:25
  • Possible duplicate of [Most efficient method to groupby on an array of objects](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects) – Michal Levý Nov 25 '19 at 07:59

1 Answers1

1

For example you can use this function:

groupBy (list, keyValue) {
const map = new Map()
list.forEach((item) => {
  const key = item[keyValue]
  const collection = map.get(key)
  if (!collection) {
    map.set(key, [item])
  } else {
    collection.push(item)
  }
})
return Array.from(map.values())  
}

then just call it

const map = groupBy(this.problemitems, 'CustomerID')

You can use function in Vue methods, then don't forget this