I have a two dimensional array:
[[bourbon, 2], [bourbon, 1],[scotch, 2]]
I would like to end up with a new array that consolidates the duplicates so that the array would become
[[bourbon, 3], [scotch, 2]]
Here is how I create the array:
for(var i=0; i< data.length; i++){
var typeW = String(data[i].doc.type);
var valueAsNumber = parseInt(data[i].doc.bottle);
typeOfWhiskey[j,i] = [typeW,valueAsNumber];
j++;
}
I have tried to check for unique values with:if(typeOfWhiskey.indexOf(typeW ) > -1){
However I am currently stuck. In this example 'typeW' is the string value for 'bourbon', or 'scotch' for example, where as 'valueAsNumber' would be 2 or 1 based on the example provided. I would like to not have to create another for look and iterate through the whole array again because I feel that would be inefficient. I think I am close but am not sure how to proceed. Thanks
typeOfWhiskey[j,i]is doing?typeOfWhiskey[j,i]is eqivalent totypeOfWhiskey[i].j,iis the comma operator at work, so the whole expression evaluates toi. You might wanttypeOfWhiskey[j][i]