Skip to content

Instantly share code, notes, and snippets.

@WangNingning1994
Last active May 25, 2023 06:24
Show Gist options
  • Save WangNingning1994/1321b7efe3b43143c0af13bf2b7d9062 to your computer and use it in GitHub Desktop.
Save WangNingning1994/1321b7efe3b43143c0af13bf2b7d9062 to your computer and use it in GitHub Desktop.
// 对象数组分类方法(Map版)
const groupBy = (items, key) => {
let map = new Map();
items.forEach(item => {
let val = item[key];
if (map.get(val)) {
map.get(val).push(item)
} else {
map.set(val, []);
map.get(val).push(item)
}
})
return map;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment