Counting elements in an array and adding them into an object
<p>Okay I researched this one and couldn't find an answer here so hopefully this isn't a duplicate. I'm also trying not to be specific in order to figure this out on my own.</p>

<pre><code>var arr = ['cat','car','cat','dog','car','dog']

function orgNums(input) {
    var count = 0;
    var obj = {};
    for (var i = 0; i &lt; input.length; i++) {
        if (input[i] === 'cat')
        obj['cat'] = count++;
    }
    return obj;
}
</code></pre>

<p>I want it to return <code>{cat:2}</code> but I'm getting <code>{cat:1}</code></p>

<p>Eventually I want it to return <code>{cat:2, car:1, dog:2, gerbil:1}</code></p>

<p>So I tried using <code>obj[cat] = ++count</code> and I'm getting the answer I want, but when I try a second if statement: <code>if input[i] === 'dog', obj[dog] = ++count</code>I get <code>{cat:2, dog:4}</code>. I noticed that it's taking what <code>count</code> already is at, 0, then moves it to 2 to count cat, then moves it to 4, taking the dog count of 2 and adding 2 to it. How do I prevent that from happening so that <code>count</code> restarts at 0 each time?</p>

<p>EDIT:
So this works beautifully 
var arr = ['cat', 'car', 'cat', 'dog', 'car', 'dog']</p>

<pre><code>function orgNums(input) {
    var obj = {};

    for (var i = 0; i &lt; input.length; i++) {
        obj[input[i]] = obj[input[i]] || 0;
        obj[input[i]]++;
    }
    return obj;
}

console.log(orgNums(arr));
</code></pre>

<p>but the final output i actually want is:</p>

<pre><code>[
{cat:1
dog:2
}
{car:2
}
]
</code></pre>

<p>So I tried throwing in an if statement like this:</p>

<pre><code>if (input[i] === 'cat'||'dog')
</code></pre>

<p>but it's still throwing <code>car</code> into the object. I'll try to figure out the multiple objects in the array. Thanks again!</p>