Multidimensional array as pivot table
I'm trying to convert this array
- var data = ["a","b","c","d","e","a","b","b","b","c","d","d","e","a"];
into
- topics [a:3,b:4,c:2,d:3,e:2] ( -- not sure how this notation should be)
but how?
this is my script, not working of obviously
- var data = ["a","b","c","d","e","a","b","b","b","c","d","d","e","a"];
- var topics = [,];
- for(i=0; i<data.length; i++){
- var arrayIndex = $.inArray(data[i], topics);
if(arrayIndex == -1) // return -1 if it is not in the list
{
topics.push([data[i],1]); // add it
}
else
{
topics[arrayIndex][1] = topics[arrayIndex][1] +1; // add 1 to existing topic
}
- }