Skip to content

Instantly share code, notes, and snippets.

@pedrohugorm
Created January 5, 2016 11:45
Show Gist options
  • Save pedrohugorm/dd9ca7a92ab3254c8f7d to your computer and use it in GitHub Desktop.
Save pedrohugorm/dd9ca7a92ab3254c8f7d to your computer and use it in GitHub Desktop.
Hierarchy Sum

Hierarchy Sum

CoffeeScript code using underscore to sum a value inside an object array recursively

A Pen by Pedro Matos on CodePen.

License.

p
h2 Coffeescript test to sum a total value in an object hierarchy
.desc Check JS/Coffeescript for code
.msg Check console for output
###
Coffeescript test to sum a total value in an object hierarchy
###
class PropostaFormViewRule
constructor: (_) ->
@_ = _
sum: (obj, key) ->
if !Array.isArray(key)
return @_.sum obj, key
#TODO: Replace angular for something else - should not depend on angular
result = angular.copy obj
for prop, i in key
result = @_.pluck result, prop
#results are array as well, if so, it needs to go recursive
if Array.isArray result[0]
#console.log "Is Array TRUE", result, key
key.splice i, 1
return @sum obj, key
return @sum(result)
vr = new PropostaFormViewRule(_)
data1 = [
{
"Nome": "Ifa 1",
"ProdutoresNacionais": [],
"ProdutoresInternacionais": [],
"ProdutoIntermediarioList": [
{
"Nome": "Produto Intermediário 1",
"Estimativa": {
"Atual": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"Ano": {
"1": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"2": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"3": {
"Total": null
},
"4": {
"Total": null
},
"5": {
"Total": null
}
}
}
},
{
"Nome": "Produto Intermediário 2",
"Estimativa": {
"Atual": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"Ano": {
"1": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"2": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"3": {
"Total": null
},
"4": {
"Total": null
},
"5": {
"Total": null
}
}
}
}
],
"Estimativa": {
"Atual": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"Ano": {
"1": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"2": {
"Total": 1,
"Unidade": 1,
"Valor": 1
},
"3": {
"Total": null
},
"4": {
"Total": null
},
"5": {
"Total": null
}
}
}
},
{
"Nome": "Ifa 2",
"ProdutoresNacionais": [],
"ProdutoresInternacionais": [],
"ProdutoIntermediarioList": [
{
"Nome": "Produto Intermediário 3",
"Estimativa": {
"Atual": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"Ano": {
"1": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"2": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"3": {
"Total": null
},
"4": {
"Total": null
},
"5": {
"Total": null
}
}
}
}
],
"Estimativa": {
"Atual": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"Ano": {
"1": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"2": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"3": {
"Total": null
},
"4": {
"Total": null
},
"5": {
"Total": null
}
}
}
}
]
data2 = [{
"Nome": "Produto Intermediário 1",
"Estimativa": {
"Atual": {
"Total": 2,
"Valor": 1,
"Unidade": 1
},
"Ano": {
"1": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"2": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"3": {
"Total": null
},
"4": {
"Total": null
},
"5": {
"Total": null
}
}
}
}, {
"Nome": "Produto Intermediário 1",
"Estimativa": {
"Atual": {
"Total": 2,
"Valor": 1,
"Unidade": 1
},
"Ano": {
"1": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"2": {
"Total": 1,
"Valor": 1,
"Unidade": 1
},
"3": {
"Total": null
},
"4": {
"Total": null
},
"5": {
"Total": null
}
}
}
}]
depth1 = ["ProdutoIntermediarioList","Estimativa", "Atual", "Total"]
depth2 = ["Estimativa", "Atual", "Total"]
test1 = vr.sum(data1, depth1)
test2 = vr.sum(data2, depth2)
console.log test1, test1 == 2
console.log test2, test2 == 4
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://yourjavascript.com/6140145215/underscore-math.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></script>
iframe {
width: 100%;
height: 1000px;
position absolute;
top: 50px;
bottom: 0;
left: 0;
right: 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment