1

I cant save array values into groups:

I have an array like this:

 ["0", "2", "8", "8", "8", "8", "8", "8", "2", "9", "9", "9", "9", "9", "2", "10", "10", "10", "10", "10", "10", "10", "10", "0", "3", "11", "11",.....]

what I need is to group values and save to object like this:

{0 = 1, 2 = 1, 8 = 6, 2 = 1, 9 = 5, 2 = 1, 10 = 9, 0 = 1,...}

I tried several ways but no luck

var max = 0;
var group = {};
for (var i = items.length; --i >= 0; ) {
    var value = items[i];
    var n = group[value] = 1 - -(group[value] | 0);
    if (n > max) {
        max = n;
    }
}

This one returns all summed values like: {0=6, 2=7,...}

This also returns me wrong result:

var j = 1,
        value = 0,
        valueArray = {};

for (i = 0; i <= items.length; i++) {
    if (value == items[i]) {
        j++;
    } else {
        valueArray[j] = value;
        j = 1;
    }
    value = items[i];
}

Any advices?

DeividasJJ
  • 125
  • 1
  • 13

4 Answers4

2

Use this...

var values = ["0", "2", "8", "8", "8", "8", "8", "8", "2", "9", "9", "9", "9", "9", "2", "10", "10", "10", "10", "10", "10", "10", "10", "0", "3", "11", "11"]

var counts = {};

_.each(values, function(v, i) {

    if (counts[v]) {
        counts[v] ++;
    } else {
        counts[v] = 1;
    }
});

console.log(counts);        

working fiddle here... http://jsfiddle.net/B8uy3/

Trent
  • 1,280
  • 11
  • 12
  • Same with jquery - just change `v` and `i` around. – eithed Jul 23 '14 at 19:25
  • Oh... good point, @eithedog. I should mention that I'm using underscore.js for the looping. The looping could be done in straight javascript too... nothing special going on there. – Trent Jul 23 '14 at 19:27
  • `values.forEach` is native Javascript. No need for underscore or jQuery here. – Johannes H. Jul 23 '14 at 19:29
  • @Trent this also returns all total values count `{0 = 2, 2 = 3,...}`, but not group count as I provided in the example `{0 = 1, 2 = 1, 8 = 6, 2 = 1, 9 = 5, 2 = 1, 10 = 9, 0 = 1,...}` – DeividasJJ Jul 23 '14 at 19:36
  • @DeividasJJ - sorry... I misunderstood the question. I don't understand that output that you provided. What's group count if not a count of each value? Can you explain? Dumb it down for me? – Trent Jul 23 '14 at 19:52
2

I think what you're trying to achieve in JS is impossible - according to what you've written, your output object would have, for example, two keys with value 0 (or three keys with value 2)

What is possible, is indeed grouping, but not according to your provided output:

let input = ["0", "2", "8", "8", "8", "8", "8", "8", "2", "9", "9", "9", "9", "9", "2", "10", "10", "10", "10", "10", "10", "10", "10", "0", "3", "11", "11"];

let output = [];
for (let i=0; i<input.length; i++)
{
    if (!output[output.length-1] || output[output.length-1].value !== input[i])
        output.push({value: input[i], times: 1})
    else
        output[output.length-1].times++;
}

console.log(output);

Fiddle: http://jsfiddle.net/UCbkz/

In the output array, you'll end up with groups of numbers as they are in your input array, so:

  • 0 => value: 0, times: 1
  • 1 => value: 2, times: 1
  • 2 => value: 8, times: 6
  • 3 => value: 2, times: 1
  • ...
Yohan W. Dunon
  • 502
  • 10
  • 18
eithed
  • 3,933
  • 6
  • 40
  • 60
0

You can use this :

var arr= ["0", "2", "8", "8", "8", "8", "8", "8", "2", "9", "9", "9", "9", "9", "2", "10", "10", "10", "10", "10", "10", "10", "10", "0", "3", "11", "11"];

var newObj = {}

for(var i = 0; i < arr.length; i++){
    newObj[arr[i]] = ++newObj[arr[i]] || 1;
}

console.log(newObj);

http://jsfiddle.net/3Fe8Q/1/

Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
  • Gagnon this returns total values count {0 = 2, 2 = 3,...}, but not group count as I provided in the example {0 = 1, 2 = 1, 8 = 6, 2 = 1, 9 = 5, 2 = 1, 10 = 9, 0 = 1,...} – DeividasJJ Jul 23 '14 at 19:38
  • @DeividasJJ well, you can't have an object with 2 times the same key... What you want (as described) is impossible... – Karl-André Gagnon Jul 23 '14 at 19:40
0

Native Javascript solution based on Trent's answer:

var values = ["0", "2", "8", "8", "8", "8", "8", "8", "2", "9", "9", "9", "9", "9", "2", "10", "10", "10", "10", "10", "10", "10", "10", "0", "3", "11", "11"]

var counts = {};

values.forEach(function (v) {
    if (counts[v]) counts[v]++;
    else counts[v] = 1;
});
Johannes H.
  • 5,875
  • 1
  • 20
  • 40