【发布时间】:2014-04-07 03:48:58
【问题描述】:
例如:
var array = ['a','a','b','b','c','c','c','c','d','d','d','d','d','d'];
var ans = array.reduce(function(acc,curr){
if(typeof acc[curr] == 'undefined') {
acc[curr] = 1;
} else {
acc[curr] += 1;
}
return acc;
}, {});
会给我:
ans = {'a':'2','b':'2','c':'4','d':'6'}
但我的目标是获得这种格式
ans = [{'word':'a','count':'2'},{'word':'b','count':'2'},{'word':'c','count':'4'},{'word':'d','count':'6'}]
任何帮助将不胜感激。
【问题讨论】:
-
@megawac OP 已经在使用
Array.prototype.reduce -
他的 sn-ps 中有语法错误,他们可以自己解决
-
我的问题不在于reduce。
标签: javascript arrays json