-2

Given array :

var arr = [ 'male01', 'woman01', 'male02', 'kid01', 'kid02', 'male06'];

How to count the number of male in that array ?

Expected result : 3.


Note: I just edited the problem to make it simpler.

Hugo LOPEZ
  • 605
  • 8
  • 21
  • 1
    Have you tried something or you want us to code for you? – Calvin Nunes May 16 '18 at 12:47
  • I have no idea where to go I guess there is some forEach or Match or something which is most convenient. I though of for loop and search/match, with incremental counter but it seems quite an heavy coding for such task. – Hugo LOPEZ May 16 '18 at 12:48
  • 1
    Possible duplicate of [Javascript: search string in array then count occurrences](https://stackoverflow.com/questions/37686802/javascript-search-string-in-array-then-count-occurrences) – Calvin Nunes May 16 '18 at 12:49
  • Possible duplicate of [Count instances of string in an array](https://stackoverflow.com/questions/9996727/count-instances-of-string-in-an-array) – Mohammad Usman May 16 '18 at 12:50
  • Given the number of -1 on my question I'am going to delete this question. Please report refer yourself to [Javascript: search string in array then count occurrences](https://stackoverflow.com/questions/37686802/javascript-search-string-in-array-then-count-occurrences) or [Count instances of string in an array](https://stackoverflow.com/questions/9996727/count-instances-of-string-in-an-array). – Hugo LOPEZ May 16 '18 at 13:19

4 Answers4

5

Try following

var arr = [ 'male01', 'woman01', 'male02', 'kid01', 'kid02', 'male06'];

console.log(arr.filter((item) => item.startsWith('male')).length);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
  • @NinaScholz - Agreeing it to be a better way. Updated. Thanks. – Nikhil Aggarwal May 16 '18 at 12:52
  • How do you know all the occurrences are at the start of the word? I would instead use `String.prototype.includes()` – Yosvel Quintero May 16 '18 at 13:02
  • @YosvelQuintero - Makes sense, however, as per the data provided, the above will work well too. Thank you. – Nikhil Aggarwal May 16 '18 at 13:04
  • Given the number of -1 on my question I'am going to delete this question. Please report your answer to [Javascript: search string in array then count occurrences](https://stackoverflow.com/questions/37686802/javascript-search-string-in-array-then-count-occurrences) or [Count instances of string in an array](https://stackoverflow.com/questions/9996727/count-instances-of-string-in-an-array) so I can +1 your elegant answer. – Hugo LOPEZ May 16 '18 at 13:18
  • No worries. Thank you for the warning – Nikhil Aggarwal May 16 '18 at 13:21
2
var arr = [ 'male01', 'woman01', 'male02', 'kid01', 'kid02', 'male06'];
var count=0;
for(var i=0;i<arr.length;i++)
{
if(arr[i].indexOf('male')>-1)
    count++;
}
2

You can also use regular expressions and the String.prototype.match()

Code:

const arr = ['male01', 'woman01', 'male02', 'kid01', 'kid02', 'male06'];
const count = arr.toString().match(/male/g).length;

console.log(count);
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
  • 1
    Given the number of -1 on my question I'am going to delete this question. Please report your answer to [Javascript: search string in array then count occurrences](https://stackoverflow.com/questions/37686802/javascript-search-string-in-array-then-count-occurrences) or [Count instances of string in an array](https://stackoverflow.com/questions/9996727/count-instances-of-string-in-an-array) so I can +1 your elegant answer. – Hugo LOPEZ May 16 '18 at 13:17
  • 1
    I have market you question as Favorite and given the +1.. But is ok, you can delete and use the solution on your personal project.. Thanks for the message – Yosvel Quintero May 16 '18 at 13:19
  • 1
    I didn't expected my answer to be down voted. Thanks for your code it's indeed the level of elegance I was looking for ! – Hugo LOPEZ May 16 '18 at 13:22
0

You need to iterate upto length of array if string found increment the value of counter. Like following.

var arr = [ 'male01', 'woman01', 'male02', 'kid01', 'kid02', 'male06'];
var stringCount = 0;
for(var i=0;i<arr.length;i++)
{
   if(arr[i].indexOf('male')>-1){
      stringCount++;
   }
}
console.log(stringCount);
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24