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?

3
  • have your heard of hashmap? Commented Jul 23, 2014 at 19:19
  • @KickButtowski YOu do realize that we're dealing with javascript here? No explicit HashMaps, but objects are implemented similar. Means he already uses them. So: How do hashmaps solve the problem? Commented Jul 23, 2014 at 19:22
  • first wall i do not know why my @ does not work. second, you are right :) Commented Jul 23, 2014 at 19:23

4 Answers 4

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
  • ...
Sign up to request clarification or add additional context in comments.

Comments

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/

5 Comments

Same with jquery - just change v and i around.
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.
values.forEach is native Javascript. No need for underscore or jQuery here.
@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 - 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?
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/

2 Comments

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 well, you can't have an object with 2 times the same key... What you want (as described) is impossible...
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;
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.