I would like to present the data in the format described in the “Aiming for” section of the page. As you can see it’s split up by “type” with the total of said type displayed below.
I know it’s possible to specifically filter an array of objects with a variable (e.g. filter type by “Type 1” and then make another filter by “Type 2”), but let’s assume that we do not know the different types until we get the data.
function uniqueFromObjectArray(arr, key){
console.log(unique);
var unique = [];
for(var i = 0; i < arr.length; i++){
if(unique.indexOf(arr[i][key]) == -1){
unique.push(arr[i][key]);
}
}
console.log(unique);
return unique;
}
Array.prototype.sum = function (prop) {
var total = 0
for ( var i = 0, _len = this.length; i < _len; i++ ) {
total += this[i][prop]
}
return total
}