在Javascript中,我试图采取数字值的初始数组,并计算其中的元素。理想情况下,结果将是两个新数组,第一个数组指定每个唯一元素,第二个数组包含每个元素出现的次数。但是,我愿意听取关于输出格式的建议。

例如,如果初始数组是:

5, 5, 5, 2, 2, 2, 2, 2, 9, 4

然后将创建两个新数组。第一个将包含每个唯一元素的名称:

5, 2, 9, 4

第二个将包含该元素在初始数组中出现的次数:

3, 5, 1, 1

因为数字5在初始数组中出现了三次,数字2出现了五次,9和4都出现了一次。

我一直在寻找解决方案,但似乎没有一个可行,而且我自己尝试过的每件事最后都出奇地复杂。任何帮助都将不胜感激!

谢谢:)


当前回答

使用ramda.js有一个更好更简单的方法可以做到这一点。 代码示例

Const ary = [5,5,5,2,2,2,2,2,2,2,2,2,9,4]; R.countBy (r = > r)(必要) countBy documentation是at documentation

其他回答

我对拉姆达的解决方案:

const testArray = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4]

const counfFrequency = R.compose(
  R.map(R.length),
  R.groupBy(R.identity),
)

counfFrequency(testArray)

链接到REPL。

如果你使用下划线,你可以走功能路线

a = ['foo', 'foo', 'bar'];

var results = _.reduce(a,function(counts,key){ counts[key]++; return counts },
                  _.object( _.map( _.uniq(a), function(key) { return [key, 0] })))

第一个数组是

_.keys(results)

第二个数组是

_.values(results)

如果本地javascript函数可用,其中大部分将默认为本地javascript函数

演示:http://jsfiddle.net/dAaUU/

所以这里是我如何做的一些最新的javascript功能:

首先,将数组缩减为一个计数的Map:

let countMap = array.reduce(
  (map, value) => {map.set(value, (map.get(value) || 0) + 1); return map}, 
  new Map()
)

通过使用Map,您的起始数组可以包含任何类型的对象,并且计数将是正确的。如果没有Map,某些类型的对象会给你奇怪的计数。 关于区别的更多信息,请参阅Map文档。

如果你所有的值都是符号、数字或字符串,这也可以用在对象上:

let countObject = array.reduce(
  (map, value) => { map[value] = (map[value] || 0) + 1; return map },
  {}
)

或者使用解构和对象扩展语法,以一种没有突变的函数式方式略显花哨:

let countObject = array.reduce(
  (value, {[value]: count = 0, ...rest}) => ({ [value]: count + 1, ...rest }),
  {}
)

此时,您可以使用Map或对象进行计数(与对象不同,Map是直接可迭代的),或者将其转换为两个数组。

有关地图:

countMap.forEach((count, value) => console.log(`value: ${value}, count: ${count}`)

let values = countMap.keys()
let counts = countMap.values()

或者对于对象:

Object
  .entries(countObject) // convert to array of [key, valueAtKey] pairs
  .forEach(([value, count]) => console.log(`value: ${value}, count: ${count}`)

let values = Object.keys(countObject)
let counts = Object.values(countObject)

返回一个可排序的数组:

let array = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4] let reducedArray = array.reduce( (acc, curr, _, arr) => { if (acc.length == 0) acc.push({item: curr, count: 1}) else if (acc.findIndex(f => f.item === curr ) === -1) acc.push({item: curr, count: 1}) else ++acc[acc.findIndex(f => f.item === curr)].count return acc }, []); console.log(reducedArray.sort((a,b) => b.count - a.count )) /* Output: [ { "item": 2, "count": 5 }, { "item": 5, "count": 3 }, { "item": 9, "count": 1 }, { "item": 4, "count": 1 } ] */

ECMAScript2015选项怎么样?

const a = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];

const aCount = new Map([...new Set(a)].map(
    x => [x, a.filter(y => y === x).length]
));
aCount.get(5)  // 3
aCount.get(2)  // 5
aCount.get(9)  // 1
aCount.get(4)  // 1

这个例子将输入数组传递给Set构造函数,创建一个唯一值的集合。然后,扩展语法将这些值展开为一个新数组,这样我们就可以调用map并将其转换为一个[value, count]对的二维数组——即如下结构:

Array [
   [5, 3],
   [2, 5],
   [9, 1],
   [4, 1]
]

然后将新数组传递给Map构造函数,得到一个可迭代对象:

Map {
    5 => 3,
    2 => 5,
    9 => 1,
    4 => 1
}

Map对象的伟大之处在于它保留了数据类型——也就是说account .get(5)将返回3,但account .get("5")将返回未定义。它还允许任何值/类型作为键,这意味着该解决方案也适用于对象数组。

函数频率(/*{数组}*/ a){ 返回新地图([…]新组(a)] . map ( X => [X, a.filter(y => y == X).length] )); } 让foo = {value: 'foo'}, Bar = {value: ' Bar '}, Baz ={值:' Baz '}; let aNumbers = [5,5,5,2,2,2,2,2,2,2,2,9,4], aObjects = [foo, bar, foo, foo, baz, bar]; 频率(许多)。forEach (val关键)= > console.log(关键 + ': ' + val)); 频率(aObjects)。forEach((val, key) => console.log(key. log)Value + ': ' + val));