javascript - 获取总值
问题描述
我可以得到我的价值观谢谢。
解决方案
假设标签最终是["Pitcher","Pitcher","Catcher","Batter"];
const tags = ["Pitcher","Pitcher","Catcher","Batter"];
const result = Object.entries(tags.reduce((r,t)=>(r[t]=(r[t]||0)+1,r),{})).map(([k,v])=>`<li key="${k}">${k} - ${v}</li>`).join('\n');
console.log(result);
当然,由于您无法以使用方式看到标签的确切内容,因此很可能标签更像
const tags = ["","Pitcher","","Pitcher","","Catcher","","Batter"];
const result = Object.entries(tags.reduce((r,t)=>(r[t]=(r[t]||0)+1,r),{})).map(([k,v])=>`<li key="${k}">${k} - ${v}</li>`).join('\n');
console.log(tags.join('')); // you can't see the empty tags here
console.log(result); // but this is the result
这就是为什么我要求你发布什么attendance
是,看看数据发生了什么
但是,按照我的建议使用过滤器...
const tags = ["","Pitcher","","Pitcher","","Catcher","","Batter"];
const result = Object.entries(tags.filter(t=>t).reduce((r,t)=>(r[t]=(r[t]||0)+1,r),{})).map(([k,v])=>`<li key="${k}">${k} - ${v}</li>`).join('\n');
console.log(tags.join('')); // you can't see the empty tags here
console.log(result); // with filter, everything is good again
推荐阅读
- optimization - 如何处理 Anylogic 中输出的可变性?
- java - 将签名的哈希嵌入到 pdf - 文档自签名后已被更改或损坏
- javascript - 如何在表单 ajax 中插入 id 参数以与 ie 11 兼容
- angular - 如何使用 jest 和 angular 对商店 onInit 进行单元测试
- javascript - 将 SVG 组转换为多个“单独的”PNG 文件
- node.js - 当这个代码块运行时,逻辑上一步一步发生了什么或者结果是如何评估的?
- java - 如何检查存储库是来自 SVN 还是 Git?
- c# - Xamarin Android 菜单项不动态显示
- c# - 如何使用带有 String.Contains 的 switch case?
- google-cloud-dataflow - 来自单个 Google Cloud DataFlow 作业的并行 DataFlow 管道