js 统计空格次数

var a = ["a","","","","b","","","c",""];
计算后的结果是
{

a:3,
b:2,
c:1

}

阅读 2.9k
2 个回答
var a = ['a', '', '', '', 'b', '', '', 'c', ''];

let b = a.reduce((d, v) => void (v ? (d.k = v) && (d.v[v] = 0) : d.v[d.k] += 1) || d, {v: {}, k: ''}).v;

console.log(b);
((a, _) => a.reduce((p, c) => 
[_ = c || _, _ && [p[_] = p[_] || 0, !c && (p[_] ++)], p][2], {}))
(["a","","","","b","","","c",""], undefined)

会忽略第一个非空字符前的所有空字符。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题