Skip to content

Instantly share code, notes, and snippets.

@ariefitriadin
Last active August 24, 2017 04:43
Show Gist options
  • Save ariefitriadin/43dec80704c5f0b42e99984d2d94cb80 to your computer and use it in GitHub Desktop.
Save ariefitriadin/43dec80704c5f0b42e99984d2d94cb80 to your computer and use it in GitHub Desktop.
Some Array Works
/* 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