Skip to content

Commit

Permalink
feat(list): getKeyValuesMap
Browse files Browse the repository at this point in the history
  • Loading branch information
novlan1 committed May 1, 2022
1 parent b555dd9 commit 3d45c04
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,50 @@ export function isListAllEqual(list = []) {

return true
}

/**
* 获取对象的value列表,并输出大对象形式
* @param {*} data
* @returns
*
* @example
*
* ```ts
* const data = [
* {
* Project: 'xx',
* Request: 111,
* Score: 111
* },
* {
* Project: 'yy',
* Request: 333,
* Score: 555
* }]
*
* getKeyValuesMap(data)
*
* // 结果为
* {
* Project: ['xx', 'yy'],
Request: [111, 333],
Score: [111, 555],
}
```
*/
export function getKeyValuesMap(data: Array<any> = []) {
if (!data.length) return {}
const keys = Object.keys(data[0])
const keyValueMap = {}

data.forEach(item => {
keys.forEach(key => {
if (keyValueMap[key]) {
keyValueMap[key].push(item[key])
} else {
keyValueMap[key] = [item[key]]
}
})
})
return keyValueMap
}

0 comments on commit 3d45c04

Please sign in to comment.