3

Can you please help me how to find the no.of images for same file name,

 var images =["Cat.png", "Cock.png","Dog.png","Parrot.png","Penguin.png",
              "Rabbit.png","Parrot.png"];

here I have 7 images in the array... I need count like..

               Cat:1   
               Parrot :2
               Penguin:1 

Please give me the suggestion

Thanks, Rajasekhar

Moons
  • 3,833
  • 4
  • 49
  • 82

3 Answers3

4

The usual solution is to use an object as a map to make the link between the keys (name of the files) and the count :

var count = {};
for (var i=images.length; i-->0;) {
   var key = images[i].split(".")[0]; // this makes 'Parrot' from 'Parrot.png'
   if (count[key]) count[key]++;
   else count[key] = 1;
}

Then you have, for example count['Parrot'] == 2

Demonstration : http://jsfiddle.net/tS6gY/

If you do console.log(count), you'll see this on the console (Ctrl+Uppercase+i on most browsers) :

enter image description here

EDIT about the i--> as requested in comment :

for (var i=images.length; i-->0;) {

does about the same thing than

for (var i=0; i<images.length; i++) {

but in the other directions and calling only one time the length of the array (thus being very slightly faster, not in a noticeable way in this case).

This constructs is often used when you have a length of iteration that is long to compute and you want to do it only once.

About the meaning of i--, read this.

i-->0 can be read as :

  • decrements i
  • checks that the value of i before decrement is strictly positive (so i used in the loop is positive or zero)
Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    That was quick. I was just typing up the same solution. I'd even named my object `count`, and used `.split()` on the strings. – nnnnnn Aug 30 '12 at 07:09
  • 1
    How does `-->` work and how efficient is it? Do you know where I could read more about `-->`, Google didn't help me much. – sQVe Aug 30 '12 at 07:18
  • 1
    @sQVe, `i--` decrements `i` by one and `i-- > 0` compares if `i` is more than zero. – Maksim Vi. Aug 30 '12 at 07:20
  • @MaksimVi. Ah, of course. DOH! Writing it that compact tricked me as I wouldn't have written it like that. Thanks for the answer. – sQVe Aug 30 '12 at 07:23
  • @dystroy Got it, the lack of spaces and the early morning tricked me. – sQVe Aug 30 '12 at 07:25
  • @sQVe I think that's the usual way to write this construct, but you'll see it more often in C code than in javascript – Denys Séguret Aug 30 '12 at 07:26
  • @dystroy I would have written it like: `for (var i = images.length; i-- > 0;)`, that's why I got confused... A minute there i thought `-->` was some kind of "until you reach" operator. I should not be on SO on early mornings :D – sQVe Aug 30 '12 at 07:28
  • Think about it : it *is* a kind of "go until you reach" operator :) – Denys Séguret Aug 30 '12 at 07:30
1

Not sure about efficiency, but this should do:

var images =["Cat.png", "Cock.png","Dog.png","Parrot.png","Penguin.png","Rabbit.png","Parrot.png"];

images.forEach(function(img){
    var count = 0;
    images.forEach(function(image, i){
        if(img === image){
            delete images[i];
            count++;
        }
    });
    console.log(img, count);          
});

DEMO

Maksim Vi.
  • 9,107
  • 12
  • 59
  • 85
0

You can keep unique keys in an array and use another array for counting the frequency:

var images =["Cat.png", "Cock.png","Dog.png","Parrot.png","Penguin.png","Rabbit.png","Parrot.png"]; 
var counts = [];
var keys = [];

for (i = 0; i < images.length; i++){
    if (!counts[images[i]]) {
        counts[images[i]] = 0;
        keys.push(images[i]);
    }
    counts[images[i]]++;
}

for (i = 0; i < keys.length; i++) alert(keys[i] + " : " + counts[keys[i]]);
​

Here is a demo : http://jsfiddle.net/e5zFC/1/

gabitzish
  • 9,535
  • 7
  • 44
  • 65