【问题标题】:How can I group(map) by data for ID?如何按 ID 数据分组(映射)?
【发布时间】:2019-11-24 12:35:31
【问题描述】:

如何按CustomerID 对我的数据进行分组?

这是行不通的,我正在尝试不同的方式,但我想我不明白这个结构

这是我的代码:

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

【问题讨论】:

标签: arrays vue.js vuejs2


【解决方案1】:

例如你可以使用这个函数:

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())  
}

那就叫它吧

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

你可以在Vue方法中使用函数,然后不要忘记this

【讨论】:

    猜你喜欢
    • 2018-06-03
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    相关资源
    最近更新 更多