javascript - Count length of items in nested js object -
given array this, how count of charts in particular category. each category can have multiple or no groups.
{ "categories":[ { "title":"category 1", "id":"cat1", "groups":[ { "title":"group 1", "id":"grp1", "charts":[ { "title":"chart 1", "id":"chart1", "type":"line" } ] } ] }, { "title":"category 2", "id":"cat2", "charts":[ { "title":"chart 2", "id":"chart2", "type":"line" } ] }, { "title":"category 3", "id":"cat3", "charts":[ { "title":"chart 3", "id":"chart3", "type":"line" } ] } ] }
is one-liner okay?
assuming data json structure:
data.categories .map(c => [ c.title, c.groups ? c.groups.map(g => g.charts.length).reduce((a, b) => a+b) : c.charts.length ])
Comments
Post a Comment