惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
博客园 - Franky
D
DataBreaches.Net
B
Blog
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
P
Proofpoint News Feed
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
月光博客
月光博客
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs
博客园 - 【当耐特】

博客园 - howhy

TypeScript any vs unknown 详细对比 TypeScript interface vs type 完整对比 html 元素包含关系 this 指向 空对象 Object.keys for ...in Reflect.ownKeys ...区别 逻辑运算符和空值运算符 运算符优先级 js 类型显式转换 js 高级函数 js 方法重载 fetch timeout js 任务顺序执行 暂停 js 并发任务 判断两个对象是否相同 js 动态拦截属性 js 通用动画 js 防抖和节流 实现 instanceof 操作符 js 单例模式 js 数组去重和扁平方法 js 继承方法 js new的过程实现 js deepCopy js '=='的隐性类型转换规则
js groupby
howhy · 2025-12-30 · via 博客园 - howhy
const users=[
  {name:'zhangsan',age:23,gender:'male'},
  {name:'lisi',age:20,gender:'female'},
  {name:'wangwu',age:18,gender:'male'},
  {name:'zhaohu',age:19,gender:'female'},
  {name:'malu',age:20,gender:'female'},
  {name:'zhouyi',age:28,gender:'male'},
  {name:'howhye',age:18,gender:'male'},
]

function groupBy(objs,generkey){
  const result={};
  for(let i=0;i<objs.length;i++){
    const item=objs[i];
    const key=generkey(item);
    if(!result[key]){
      result[key]=0;
    }
    result[key]+=1;
  }
  return result;
}
console.log(groupBy(users,(obj)=>obj.gender))
console.log(groupBy(users,(obj)=>obj.age>20?'>20':"<=20"))
console.log(groupBy(users,(obj)=>obj.name.length))