Last active
August 24, 2017 04:43
-
-
Save ariefitriadin/43dec80704c5f0b42e99984d2d94cb80 to your computer and use it in GitHub Desktop.
Some Array Works
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Example Array Of Objects : */ | |
let anymalgroups = [ | |
{nama:"kambing", jenis: "mamalia"}, | |
{nama:"kerbau", jenis: "mamalia"}, | |
{nama:"bebek", jenis: "unggas"}, | |
{nama:"ayam", jenis: "unggas"} | |
] | |
/* Count Per Jenis using Array.reduce in es2015 style */ | |
let jmlperjenis = anymalgroups.reduce((grup, arr) => { | |
grup[arr.nama] = (grup[arr.nama] || 0) + 1 | |
return grup | |
},{}) | |
/* result : | |
mamalia: 2, | |
unggas: 2 | |
*/ | |
/* count per specific Jenis using Array.reduce in es2015 style */ | |
let jmlUnggas = anymalgroups.reduce((n,arr) => { | |
return n + (arr.jenis === 'unggas') | |
},0) | |
/* result : 2 */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment