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

例如,如果初始数组是:

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

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

5, 2, 9, 4

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

3, 5, 1, 1

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

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

谢谢:)


当前回答

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));

其他回答

根据@adamse和@pmandell的回答(我支持),在ES6中,你可以在一行中完成:

2017年编辑:我使用||来减少代码大小,使其更具可读性。

var =[7, 1、7、2、2、7、3、3、3、7、7、7、7]; 警报(JSON.stringify ( a.reduce ((r, k) = > {[k] = 1 + r [k] | | 1;返回r}, {}) ));


它可以用来计数字符:

var s =“唵嘛呢叭咪吽”; 警报(JSON.stringify ( s.split(”)。Reduce ((a, c)=>{a[c]++?0:a[c]=1;返回a},{}) ));

返回一个可排序的数组:

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 } ] */

给定下面提供的数组:

const array = [ 'a', 'b', 'b', 'c', 'c', 'c' ];

你可以使用这个简单的一行代码来生成一个哈希映射,将一个键链接到它在数组中出现的次数:

const hash = Object.fromEntries([ ...array.reduce((map, key) => map.set(key, (map.get(key) || 0) + 1), new Map()) ]);
// { a: 1, b: 2, c: 3 }

扩展和解释:

// first, we use reduce to generate a map with values and the amount of times they appear
const map = array.reduce((map, key) => map.set(key, (map.get(key) || 0) + 1), new Map())

// next, we spread this map into an array
const table = [ ...map ];

// finally, we use Object.fromEntries to generate an object based on this entry table
const result = Object.fromEntries(table);

这个数组归功于@corashina。减少代码

所以这里是我如何做的一些最新的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)

Const data = [5,5,5,2,2,2,2,2,2,2,2,9,4] 函数计数(arr) { 加勒比海盗。减少((上一页,咕咕叫)= >(上一页[咕咕叫]= + +上一页[咕咕叫]| | 1,上一页),{}) } console.log (count(数据)