underscore.js - _.groupBy nested attribute

萝らか妹 提交于 2019-11-29 06:57:04

The second argument for groupBy can be a function or a string:

groupBy _.groupBy(list, iterator)

Splits a collection into sets, grouped by the result of running each value through iterator. If iterator is a string instead of a function, groups by the property named by iterator on each of the values.

You want to use the function form since you're not grouping on a simple top-level property:

_(data).groupBy(function(o) {
    return o.location[0].name;
});

Demo: http://jsfiddle.net/ambiguous/YFKXC/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!