首页 > 解决方案 > 获取总值

问题描述

我可以得到我的价值观谢谢。

标签: javascriptreactjsreact-nativereact-redux

解决方案


假设标签最终是["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


推荐阅读