-3

I'm interested in finding out how many times every object in an array occurs.

Here's what I want to do mixed with some pseudo-code of what I think is the way to do it.

var myArray = [1,2,3,1,1,3,4];

for (i = 0; i < myArray.length; i++) {
    if (myArray[i].AlreadyExistsInAVariable) {
        var[i]+ = 1;
    }
    else {
        CreateAnewVar();
    }
}

var one = 3;
var two = 1;
var three = 2;
var four = 1;
moffeltje
  • 4,521
  • 4
  • 33
  • 57
Fyxerz
  • 169
  • 1
  • 14
  • Also to bear in mind, the amount of objects in the array will vary constantly, as well as the amount of types of them. – Fyxerz Jul 24 '15 at 13:43
  • 3
    almost a duplicate: http://stackoverflow.com/q/5667888/588079. Solving the problem Fyxerz pointed out would however make it a duplicate. – GitaarLAB Jul 24 '15 at 13:44
  • Why don't you implement it and test it and improve it until you made it on your own? – moffeltje Jul 24 '15 at 13:44
  • I think you need instead something like this: http://jsfiddle.net/fh0pzxeu/ – A. Wolff Jul 24 '15 at 13:50

3 Answers3

0

You should use object to count the occurence of numbers in the array rather than using variables.

var myArray = [1,2,3,1,1,3,4];
var counter = {};
for (i = 0; i < myArray.length; i++) {
    if (myArray.indexOf(myArray[i])>=0) {
        if(!counter[myArray[i]]){
           counter[myArray[i]]=1;
        }
        else{
           counter[myArray[i]]++;  
        }    
    }
}
console.log(counter);
Gopinath Shiva
  • 3,822
  • 5
  • 25
  • 48
  • 1
    why this answer is down voted. Providing reason before downvoting is appreciated – Gopinath Shiva Jul 24 '15 at 13:47
  • this is pretty much exactly how I would do it. But I don't think you need the line `if (myArray.indexOf(myArray[i])>=0)` it's just redundant. – Buzz Jul 24 '15 at 13:49
  • 1
    SO is already overflown by these type of questions! Also it seems OP did not put any efforts to atleast search before posting it on SO. – Vicky Gonsalves Jul 24 '15 at 13:51
  • @Vicky: so is that the valid reason for down voting? – Gopinath Shiva Jul 24 '15 at 13:52
  • I personally did not downvote. But I think it would be fair enough for who did this.. – Vicky Gonsalves Jul 24 '15 at 13:53
  • 1
    I do believe the people would downvote the answer only when it is wrong or irrevelant. Downvoting without reason like this would discourage the people to answer or help the questionaire, which I dont feel correct. – Gopinath Shiva Jul 24 '15 at 13:56
0

How about this:

var myArray = [1,2,3,1,1,3,4];

var count = myArray.reduce(function(n, val) {
    return n + (val === 1);
}, 0);

console.log(count);

Edit:

For counting occurrences of every element in an array i would do something like this:

var result = {};

[1,2,3,1,1,3,4].forEach(function(e, i) {
     result[e] = result[e] + 1 || 1;
});

alert(JSON.stringify(result, null, "\t"));
brroshan
  • 1,640
  • 17
  • 19
  • 2
    I can't even begin to fathom what this is trying to do... – jmar777 Jul 24 '15 at 13:50
  • Well, technically it counts the number of occurrences of the number 1, but even then only due to the bizarre type-coercion behavior in JavaScript that causes ` + true` to equal ` + 1`. – jmar777 Jul 24 '15 at 13:52
  • I didn't read the question correctly. Thought the OP wanted to count the occurrence of a single element. Care to explain exactly what you mean @jmar777? Doesn't `===` compare without coercing? – brroshan Jul 24 '15 at 14:54
  • Yes, `===` performs strict equality comparisons, but the result of that comparison is a boolean. Therefore, the `n + (val === 1)` expression results in a ` + ` expression. I don't know whether it was by design or not, as your solution *technically works* (!!), but that's only due to the fact that, in JavaScript ` + true` results in ` + 1` as a result. – jmar777 Jul 24 '15 at 15:34
-1

You can't actually just create new variable bindings like that. You can, however, dynamically add new properties to an object, which is probably a better match for this problem. Here's an example that counts up the occurrences of numbers in that array:

var myArray = [1,2,3,1,1,3,4];

var counts = myArray.reduce(function(result, item) {
    result[item] || (result[item] = 0);
    result[item]++;
    return result;
}, {});

console.log(counts);

See it in action here: http://jsbin.com/huduroyite/edit?js,console

jmar777
  • 38,796
  • 11
  • 66
  • 64
  • Would appreciate an explanation for the downvote... – jmar777 Jul 24 '15 at 13:49
  • This answer works and seems like a great way of solving my problem, better than working with arrays. However, I don't really understand how you do it... Is there any link I can infor myself a bit more about the function within the reduce? – Fyxerz Jul 24 '15 at 14:06
  • 1
    @Fyxerz The MDN docs are pretty good: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce – jmar777 Jul 24 '15 at 14:15