使用下划线对数组中的嵌套对象进行排序

The*_*ebs 4 javascript arrays sorting underscore.js

考虑以下扩展对象:

在此输入图像描述

现在,每个这些对象都存储在一个数组中,并且可以通过名称,电子邮件,created_at或其他任何内容轻松排序.但是,如果我想通过用户个人资料投资者类型疼痛呢.在图像中你会做:data.profile.data.investor_type获得投资者类型.

如何使用下划线通过对象中的嵌套属性对这些对象的数组进行排序?

the*_*eye 14

您可以使用_.sortBy您显示的确切代码

_.sortBy(data, function(obj) { return obj.profile.data.investor_type; });
Run Code Online (Sandbox Code Playgroud)

如果您的环境支持ECMA Script 2015的Fat-arrow功能,那么您可以将其编写为

_.sortBy(data, (obj) => obj.profile.data.investor_type);
Run Code Online (Sandbox Code Playgroud)