最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find number of occurences of string elements in an Array using lodash or underscore js - Stack Overflow

programmeradmin0浏览0评论

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 weibo inside that array. eg:

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

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 weibo inside that array. eg:

{
  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 };
});
Share Improve this question edited May 21, 2015 at 12:18 Adam Boduch 11.2k3 gold badges31 silver badges38 bronze badges asked May 21, 2015 at 10:59 DibishDibish 9,29322 gold badges68 silver badges107 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 17

Here's how you could do it using 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 page from the end of the string. Notice how there's no id prefix since it's just being passed the result of the function in front of it.

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论