以惯用方式查找给定值在数组中出现的次数

编程入门 行业动态 更新时间:2024-10-11 23:22:45
本文介绍了以惯用方式查找给定值在数组中出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个重复值的数组。我想找到任何给定值的出现次数。

I have an array with repeating values. I would like to find the number of occurrences for any given value.

例如,如果我有一个如此定义的数组: var dataset = [2,2,4,2,6,4,7 ,8]; ,我想找到数组中某个值的出现次数。也就是说,程序应该显示如果我有3次出现的值 2 ,则出现1次值 6 ,等等。

For example, if I have an array defined as so: var dataset = [2,2,4,2,6,4,7,8];, I want to find the number of occurrences of a certain value in the array. That is, the program should show that if I have 3 occurrences of the value 2, 1 occurrence of the value 6, and so on.

最惯用/优雅的方法是什么?

What's the most idiomatic/elegant way to do this?

推荐答案

reduce 在这里比 filter 更合适,因为它不构建一个临时数组计数。

reduce is more appropriate here than filter as it doesn't build a temporary array just for counting.

var dataset = [2,2,4,2,6,4,7,8]; var search = 2; var count = dataset.reduce(function(n, val) { return n + (val === search); }, 0); console.log(count);

请注意,扩展它以使用自定义匹配谓词很容易例如,计算具有特定属性的对象:

Note that it's easy to extend that to use a custom matching predicate, for example, to count objects that have a specific property:

people = [ {name: 'Mary', gender: 'girl'}, {name: 'Paul', gender: 'boy'}, {name: 'John', gender: 'boy'}, {name: 'Lisa', gender: 'girl'}, {name: 'Bill', gender: 'boy'}, {name: 'Maklatura', gender: 'girl'} ] var numBoys = people.reduce(function (n, person) { return n + (person.gender == 'boy'); }, 0); console.log(numBoys);

计算所有项目,即制作像 {x:count of xs} 这样的对象在javascript中很复杂,因为对象键只能是字符串,因此您无法可靠地计算具有混合类型的数组。不过,以下简单的解决方案在大多数情况下都能正常运行:

Counting all items, that is, making an object like {x:count of xs} is complicated in javascript, because object keys can only be strings, so you can't reliably count an array with mixed types. Still, the following simple solution will work well in most cases:

count = function (ary, classifier) { classifier = classifier || String; return ary.reduce(function (counter, item) { var p = classifier(item); counter[p] = counter.hasOwnProperty(p) ? counter[p] + 1 : 1; return counter; }, {}) }; people = [ {name: 'Mary', gender: 'girl'}, {name: 'Paul', gender: 'boy'}, {name: 'John', gender: 'boy'}, {name: 'Lisa', gender: 'girl'}, {name: 'Bill', gender: 'boy'}, {name: 'Maklatura', gender: 'girl'} ]; // If you don't provide a `classifier` this simply counts different elements: cc = count([1, 2, 2, 2, 3, 1]); console.log(cc); // With a `classifier` you can group elements by specific property: countByGender = count(people, function (item) { return item.gender }); console.log(countByGender);

在ES6中,您可以使用 Map 对象来可靠地使用计算任意类型的对象:

In ES6, you use the Map object to reliably count objects of arbitrary types:

class Counter extends Map { constructor(iter, key=null) { super(); this.key = key || (x => x); for (let x of iter) { this.add(x); } } add(x) { x = this.key(x); this.set(x, (this.get(x) || 0) + 1); } } // again, with no classifier just count distinct elements results = new Counter([1, 2, 3, 1, 2, 3, 1, 2, 2]); for (let [number, times] of results.entries()) console.log('%s occurs %s times', number, times); // counting objects people = [ {name: 'Mary', gender: 'girl'}, {name: 'John', gender: 'boy'}, {name: 'Lisa', gender: 'girl'}, {name: 'Bill', gender: 'boy'}, {name: 'Maklatura', gender: 'girl'} ]; chessChampions = { 2010: people[0], 2012: people[0], 2013: people[2], 2014: people[0], 2015: people[2], }; results = new Counter(Object.values(chessChampions)); for (let [person, times] of results.entries()) console.log('%s won %s times', person.name, times); // you can also provide a classifier as in the above byGender = new Counter(people, x => x.gender); for (let g of ['boy', 'girl']) console.log("there are %s %ss", byGender.get(g), g);

更多推荐

以惯用方式查找给定值在数组中出现的次数

本文发布于:2023-11-30 22:09:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   次数   定值   方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!