I am currently working on counting the frequency of elements in an array using a for loop with arrays of objects. The code prints the correct output.
counts = {};
counter = 0;
counter_array = [50,50,0,200]; //this example array is dynamically filled
for (var x = 0, y = counter_array.length; x < y; x++) {
counts[counter_array[x]] = (counts[counter_array[x]] || 0) + 1;
}
console.log('FREQUENCY: ',counts); //output should be FREQUENCY: {50:2, 0:1, 200:1}
Now, I have another array containing arrays:
holder_text_array = [["a",50,0],["b",0,0]]; //example of a dynamically filled array
var p = "a";
var i = 0;
while(i < holder_text_array.length){
if (holder_text_array[i][0]==p) {
var s = counts[holder_text_array[i][1]];
console.log('Element: ', holder_text_array[i][1]); //outputs 50 when i = 0
console.log('frequency: ',counts[s]); //prints undefined
counter = counts[s];
}
i++;
}
The array of arrays named "holder_text_array" contains elements whose frequencies I would like to obtain within this while loop. Can someone help me identify where my code may be incorrect?