arrays - 如何按 ID 数据分组(映射)?
问题描述
我如何对我的数据进行分组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
}
})
}
解决方案
例如,您可以使用此功能:
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
推荐阅读
- python - python2到python3修改源码过程中出现的问题
- windows - “抱歉,此文件无效,无法显示。” 在 GIthub 上使用 pdf 文件
- xcode - 如何在cordova项目中通过config.xml更改ios xcode“Packaging”->“Product Name”?
- reactjs - 每个组件的 useHistory 默认值
- fabricjs - 如何在没有settimeout的情况下制作多步fabric.js动画?
- ldap - LDAP 和用户服务是否相同?
- flutter - 解析来自 API 调用的响应
- mysql - 如何在 MySQL 中创建列而不是连接字符串?
- c - How to convert a char string into int string?
- ios - 使用参数请求数据 Alamofire SwiftUI