-1

I have this JavaScript array with length 129.

var fullnames = [Karri, Ismo, Grigori, Ahmed, Roope, Arto .....]

I would like to find how many times those names appeared in an array and store that information in an array like this:

var counter = [2, 5, 7, ..]

where Karri occured in fullnames array 2 times, Ismo occured 5 times etc. Any ideas about how to do it?

supaplex
  • 157
  • 4
  • 18
  • 2
    Have you tried anything? – j08691 Apr 14 '15 at 18:37
  • The most direct answer is probably what your problem set question is looking for: use [`reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce). Give it a shot and post your work back here if you can't figure it out. – Jason Cust Apr 14 '15 at 18:42
  • reduce is not enough, there needs to be a nested iteration of some sort. – dandavis Apr 14 '15 at 18:43
  • @dandavis I wasn't giving a full answer hence the comment. This is to get the OP going so they can attempt to solve it on their own. – Jason Cust Apr 14 '15 at 18:44
  • grr, mis-closed: i think you want to count an array not shown? `var fullNames = ['Karri', 'Ismo', 'Grigori', 'Ahmed'], counts=[], whole=['Karri', 'Ahmed', 'Ahmed', 'Ismo', 'Grigori', 'Ahmed','Karri', 'Ismo', 'Grigori', 'Ahmed', 'Ismo', 'Grigori' ] whole.forEach(function(name){ var slot=fullNames.indexOf(name); counts[slot]=counts[slot]||0; counts[slot]++; }); counts // == [2, 3, 3, 4]` – dandavis Apr 14 '15 at 18:49

3 Answers3

3

This is the best - and simple - way I can think of:

var fullnames = ["Karri", "Ismo", "Grigori", "Ahmed", "Roope", "Ahmed", "Karri", "Arto", "Ahmed"];
var counts = {};

for (var i = 0; i < fullnames.length; i++)
{
    if (!counts.hasOwnProperty(fullnames[i]))
    {
        counts[fullnames[i]] = 1;
    }
    else 
    {
        counts[fullnames[i]]++;
    }
}

console.log(counts);

Original Fiddle.

Using an array to store the counts doesn't makes much sense, so I used an object instead.

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
  • so where does the array of data to be searched plug in? – dandavis Apr 14 '15 at 18:40
  • @dandavis sorry but what you mean? – DontVoteMeDown Apr 14 '15 at 18:41
  • i think he wants to count each of those names in a big array of names and then return an ordered count array in the same order as the list of names to search for in the big array (not shown) – dandavis Apr 14 '15 at 18:42
  • @dandavis sure but that doens't makes much sense(at least the OP didn't show us a purpose on that). Look [this fiddle](http://jsfiddle.net/dr75vafL/) I've made from techfoobar example. If you add an unique string in the sequence it breaks up the result. Unless if you repeat the count for each position of each string to know which count is relative to which string, what I think is more complex. – DontVoteMeDown Apr 14 '15 at 18:47
  • well, the way i figured would explain the desired ordered array output, thowing away indexed wouldn't make sense in that scenrio. maybe it was just a bad question... – dandavis Apr 14 '15 at 18:52
1

I am assuming that fullnames is array of strings. If so, you can do it like so:

var occurences = { };
for (var i = 0; i < fullnames.length; i++) {
    if (typeof occurences[fullnames[i]] == "undefined") {
        occurences[fullnames[i]] = 1;
    } else {
        occurences[fullnames[i]]++;
    }
}

console.log(occurences); // Prints out something like: {"Karri": 2, "Ismo": 5, ...}
leopik
  • 2,323
  • 2
  • 17
  • 29
0
var fullnames = ['Karri', 'Ismo', 'Grigori', 'Karri', 'Ismo', 'Grigori', 'Grigori', 'Karri', 'Ismo', 'Grigori', 'Grigori'];
var counts = [];
fullnames.forEach(function(_item) {
    if(typeof counts[_item] === 'undefined') counts[_item] = 1;
    else counts[_item]++;
});
var result = [];
for(i in counts) result.push(counts[i]);
console.log(result);

// outputs [3, 3, 5]
techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • 1
    I think that storing the result in an array doesn't makes much sense. What if you have an different string after the second `Karri` which doens't repeats ? What count is relative to it ? http://jsfiddle.net/dr75vafL/ – DontVoteMeDown Apr 14 '15 at 18:44