Skip to content

Instantly share code, notes, and snippets.

@amalshehu
Created May 28, 2017 08:49
Show Gist options
  • Save amalshehu/dd18741a9bf8eb6d9666f5b4eb5e4c02 to your computer and use it in GitHub Desktop.
Save amalshehu/dd18741a9bf8eb6d9666f5b4eb5e4c02 to your computer and use it in GitHub Desktop.
Find total assets
//Assets calculator
let people = [{
"id": 1, // Person Id
"name": "Amal Shehu", // Name of person
"date": "2017-5-27", // Date
"totalAssets": 500, // Sum of total (gems collected)
"gems": [{ // Gems collected from forest
"total": 500, // totalCollected * value
"value": 100, // Value of one gem (Ruby = 100)
"totalCollected": 5, // Total number
"gemName": "Ruby", // Gem name
"gemCode": "GEM001" // Gem code
}]
}, {
"id": 2,
"name": "Jikku",
"date": "2017-5-28",
"totalAssets": 1300, // TotalAssets = Emerald.total + Moonstone.total
"gems": [{
"total": 900,
"value": 150,
"totalCollected": 6,
"gemName": "Emerald",
"gemCode": "GEM002"
}, {
"total": 400,
"value": 200,
"totalCollected": 2,
"gemName": "Moonstone",
"gemCode": "GEM003"
}]
}, {
"id": 3,
"name": "Rakesh",
"date": "2017-5-29",
"totalAssets": 2900,
"gems": [{
"total": 1500,
"value": 150,
"totalCollected": 10,
"gemName": "Emerald",
"gemCode": "GEM002"
}, {
"total": 1000,
"value": 200,
"totalCollected": 5,
"gemName": "Moonstone",
"gemCode": "GEM003"
},
{
"total": 400,
"value": 100,
"totalCollected": 4,
"gemName": "Ruby",
"gemCode": "GEM001"
}
]
}]
computeTotal = () => {
// Target object
// "totalAssets": Total assets of all people ie,people[0].totalAssets + people[1]
// "gemsValueTotal":[{
// "Ruby": Total ruby value collected by people ie, people[0].gems[0].total + people[1]...
// "Emerald": ...,
// "Moonstone: ...",
// "Diamond" : ..., will be 0. Because no Diamond collected.
// }]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment