I'm trying to make a function that will get every key "quantidade", take their values, and multiply for the corresponding "tecnologia" key of the parent key, if the object has the "A" key in "tecnologia", it will multiply for the "A" value in the first object, this is what i did so far:
function calcula (tabela, arvore) {
for (i = 0; i < arvore.length; i++) {
arrFilhos = [`["filhos"][x]`]
arrArvore = [`arvore[i]`]
arrLevel = []
for(x = 0; x < arvore[i]["filhos"].length; x++){
if ("filhos" in eval(arrArvore[0])){
console.log(eval(arrArvore.toString().replace(/,/g , "")))
arrArvore.push(arrFilhos[0])
}
}
}
}
I'm assuming that tecnologia only exists in the last node
function calculaCusto (tabelaCusto, arvoreArquitetura) {
const cb = (prev, next) => {
if(next.tecnologia) {
return prev + next.quantidade * tabelaCusto[next.tecnologia];
}
else if(next.filhos){
return prev + next.quantidade * next.filhos.reduce(cb, 0);
} else { //doesn't have tecnologia nor filhos
return;
}
}
return arvoreArquitetura.reduce(cb, 0);
}