Group Array with count

前端 未结 3 589
醉酒成梦
醉酒成梦 2021-01-15 08:19

I have an array of items that contains several properties. One of the properties is an array of tags. What is the best way of getting all the tags used in those items and or

3条回答
  •  爱一瞬间的悲伤
    2021-01-15 09:04

    Map all tag arrays into a single array, and then countBy

    var tags = _.flatten(_.map(items,d=>d.tags))
    tags = _.countBy(tags)
    

    Using underscores chain utility

    var tags = _.chain(items).map(d=>d.tags).flatten().countBy().value();
    

提交回复
热议问题