Created
May 23, 2023 05:46
-
-
Save WangNingning1994/1e1af4c38c53da7c72f790f130640ef5 to your computer and use it in GitHub Desktop.
对象数组的分类方法(依照某个key)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 对象数组分类方法 | |
const groupBy = (items, key) => items.reduce( | |
(result, item) => ({ | |
...result, | |
[item[key]]: [ | |
...(result[item[key]] || []), | |
item, | |
], | |
}), | |
{}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment