Michael Unterthurner
Michael Unterthurner

Reputation: 941

Array with counted items of a other array

I have a array, in this array will be saved a JavaScript object.

var items = [];

var item = {
   id: data.id,
   kategorie: data.kategorie,
   name: data.name
}

items.push(item);        // that will be done few times

Now i have items like this

items[0] = id: 1, kategorie: "scheduled", name: "Test1"
items[1] = id: 2, kategorie: "queued", name: "Test2"
items[2] = id: 3, kategorie: "recorded", name: "Test3"
items[3] = id: 4, kategorie: "recorded", name: "Test4" 
items[4] = id: 5, kategorie: "recorded", name: "Test5"

I wanna count this array. At least the final array should be like this.

arrayFinal[0] = kategorie: "scheduled", counted: 1
arrayFinal[1] = kategorie: "recorded", counted: 3

How I do that?

Upvotes: 0

Views: 101

Answers (5)

user663031
user663031

Reputation:

Look into underscore.js's _.countBy routine:

counts = _.countBy(items, "kategorie");

That will create an object looking like this:

{ 
    recorded : 3,
    ...
}

To convert that into the array format, you want _.map:

arrayFinal = _.map(counts, function(v,k){
    return {kategorie: k, counted: v};
});

There, you're done.

Upvotes: 1

nicosantangelo
nicosantangelo

Reputation: 13716

var items = [
    { id: 1, kategorie: "scheduled", name: "Test1" },
    { id: 2, kategorie: "queued", name: "Test2" },
    { id: 3, kategorie: "recorded", name: "Test3" },
    { id: 4, kategorie: "recorded", name: "Test4" },
    { id: 5, kategorie: "recorded", name: "Test5" }
];

var index, 
    arrayFinal = [];
arrayFinal.contains = function(kategorie) {
    for(var i = 0, len = arrayFinal.length; i < len; i++) {
        if(arrayFinal[i].kategorie === kategorie) {
            return i;
        }
    }
    return false;
};

for(var i = 0, len = items.length; i < len; i++) {
    if( index = arrayFinal.contains(items[i].kategorie)) {
        arrayFinal[index].counted++;
    } else {
       arrayFinal.push({ kategorie: items[i].kategorie, counted: 1 });
    }
}

Upvotes: 1

speti43
speti43

Reputation: 3046

If you are familiar with linq you can write a query. Group by kategorie, count IDs.

LINQ for JavaScript

Eg.:

 var queryResult = Enumerable.From(items)    
.GroupBy("$.kategorie", "", 'key,e=>key+":"+e.Count()', "").ToJSON();

Upvotes: 1

wassup
wassup

Reputation: 2503

If you thought a bit and experiment, you'd do it.

  1. Create an associative array kategorieCount.
  2. Iterate over items. If there is kategorieCount[items[i].name] increment it, if not – create the element at i and then increment value.
  3. Create an array arrayFinal.
  4. Iterate over kategorieCount. Put the key in arrayFinal[i].kategorie and the value in arrayFinal[i].counted.

Upvotes: 0

rink.attendant.6
rink.attendant.6

Reputation: 46208

This doesn't produce your exact output, but it gives you an idea of what to do:

var items = [],
    arrayFinal = [];

items[0] = {
    id: 1,
    kategorie: "scheduled",
    name: "Test1"
};
items[1] = {
    id: 2,
    kategorie: "queued",
    name: "Test2"
};
items[2] = {
    id: 3,
    kategorie: "recorded",
    name: "Test3"
};
items[3] = {
    id: 4,
    kategorie: "recorded",
    name: "Test4"
};
items[4] = {
    id: 5,
    kategorie: "recorded",
    name: "Test5"
};

items.forEach(function (v, i) {
    if (typeof arrayFinal[v.kategorie] === 'undefined') {
        arrayFinal[v.kategorie] = 1;
    } else {
        arrayFinal[v.kategorie] += 1;
    }
});

Upvotes: 2

Related Questions