javascript 使用 lodash 或下划线 js 查找数组中字符串元素的出现次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30371785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:06:12  来源:igfitidea点击:

Find number of occurences of string elements in an Array using lodash or underscore js

javascriptunderscore.jslodash

提问by Dibish

I have an array in the following format:

我有以下格式的数组:

var array = [ 
  { id: '555weibo' },
  { id: '578weibo' },
  { id: '111facebook' },
  { id: '123facebook' },
  { id: '145facebookpage' },
  { id: '145facebookpage' },
  { id: '766facebook' },
  { id: '242facebook' },
  { id: '432twitter' },
  { id: '432exing' }
 ];

I need to find the number of occurrences of facebook, twitter, xing, and weiboinside that array. eg:

我需要找到facebook, twitter, xing, 和weibo在该数组中出现的次数。例如:

{
  weibo: 2,
  facebook: 6,
  twitter: 1,
  xing: 1
}

I've searched for a solution, but nothing seems to work.

我已经寻找了解决方案,但似乎没有任何效果。

The following code doesn't give the expected result. Your help is much appreciated. Thank you.

下面的代码没有给出预期的结果。非常感谢您的帮助。谢谢你。

var filtered = _.map(diff, function(value, key) {
   return { id: key, count:value };
});

采纳答案by Kris Ku

With using underscore:

使用下划线:

var services = ['weibo', 'facebook', 'twitter', 'xing'];
var result = _.map(services, function(service){ 
     var length = _.reject(array, function(el){
           return (el.id.indexOf(service) < 0); 
     }).length; 
     return {id: service, count: length};
});

回答by Adam Boduch

Here's how you could do it using lodash:

以下是使用 lodash 的方法:

_.countBy(array, _.flow(
    _.method('id.replace', /^\d+/, ''),
    _.method('replace', /page$/, '')
));
  • countBy()returns an object with counts as values. The keys are generated by the passed in callback, so this is where you can parse and manipulate the keys you want to count by.
  • flow()generates a function, usually for callbacks to map()and so on. It takes an arbitrary number of functions as arguments. The input is filtered through each of these functions and the result is returned. This is a powerful way to compose callbacks because it's easy to rearrange/add/remove it's parts.
  • method()returns a function that's invoked on the given object. You want to replace the beginning numbers with an empty string here.
  • method()is doing the same thing here, except it's removing pagefrom the end of the string. Notice how there's no idprefix since it's just being passed the result of the function in front of it.
  • countBy()返回一个以计数为值的对象。键由传入的回调生成,因此您可以在此处解析和操作要计数的键。
  • flow()生成一个函数,通常用于回调map()等等。它接受任意数量的函数作为参数。通过这些函数中的每一个过滤输入并返回结果。这是一种编写回调的强大方法,因为它很容易重新排列/添加/删除它的部分。
  • method()返回在给定对象上调用的函数。您想在此处用空字符串替换开头的数字。
  • method()在这里做同样的事情,除了它page从字符串的末尾删除。请注意没有id前缀,因为它只是传递了它前面的函数的结果。

回答by Denys Séguret

You must specify the possible keys as there's no way to guess them in the initial array.

您必须指定可能的键,因为无法在初始数组中猜测它们。

Here's a solution, you don't need any library:

这是一个解决方案,您不需要任何库:

var filtered = array.reduce(function(m,v){
    for (var k in m) if (~v.id.indexOf(k)) m[k]++;
    return m;
},{facebook:0, weibo:0, twitter:0, xing:0});

Demonstration

示范