JSFiddle - React, Tailwind, and code Playground
by pierian_design
HTML
<div id="out"></div>
JavaScript
var res = [
[[1, 2], [5, 6, 700], [9, 10, 11, 12]],
[[10, 20, 30], [50, 60, 70, 80]],
[['', 200, 300, 400], [500, 600], [900, 1000], [1100, 1200]]
]
/*-------------------------------------------------------------*/
function callSum(acc, i, j, v) {
var count = isNumber(v);
if(count > 0){
if(!acc[i][j]){
acc[i][j] = v;
}else{
acc[i][j] = acc[i][j] + v;
}
}
return acc;
}
function callCount(acc, i, j, v) {
var count = isNumber(v);
if(count > 0){
if(!acc[i][j]){
acc[i][j] = count;
}else{
acc[i][j]++;
}
}
return acc;
}
function callCounta(acc, i, j, v) {
var count = 1;
if(!acc[i][j]){
acc[i][j] = count--;
}else{
acc[i][j]++;
}
return acc;
}
function callAverage(acc, i, j, v) {
var count = isNumber(v);
if(count > 0){
if(!acc[i][j]){
acc[i][j] = {value: v, count: count};
}else{
acc[i][j].value = acc[i][j].value + v;
if(count > 0){
acc[i][j].count++;
}
}
}
return acc;
}
function isNumber(v) {
if (typeof v == "number") {
return 1;
} else {
return 0;
}
}
function prepareValues(arrs, func){
return arrs.reduce(function(acc, arr){
if(Array.isArray(arr)){
arr.forEach(function(subarr, i){
if(Array.isArray(subarr)){
if(!Array.isArray(acc[i])){
acc[i] = [];
}
subarr.forEach(function(v, j){
func(acc, i, j, v);
});
}
});
}
return acc;
},[]);
}
/*------------------------------------------------------------*/
/*-----------------------РЕЗУЛЬТАТЫ---------------------------*/
var sum = prepareValues(res, callSum);
var...