使用下划线到阵列中的嵌套对象进行排序 [英] Using underscore to sort nested objects in an array
问题描述
考虑下面的扩展对象:
现在每个对象存储到一个数组,它很容易通过姓名,电子邮件,created_at或什么别的曾经进行排序。但是,如果我想疼了用户的个人资料投资者类别的内容。在图像你会怎么做: data.profile.data.investor_type
来获得投资者的类型。
Now each of these objects is stored into an array and its easy to sort by name, email, created_at or what ever else. But what about if I wanted to sore by the users profile investor type. In the image you would do: data.profile.data.investor_type
to get the investor type.
我如何使用下划线进行排序这些对象通过嵌套属性的对象数组?
How do I use underscore to sort an array of these objects by nested attributes in the object?
推荐答案
您可以使用 _。 sortBy
具有确切code你有节目
You can use _.sortBy
with the exact code you have show
_.sortBy(data, function(obj) { return obj.profile.data.investor_type; });
如果您的环境支持 ECMA脚本2015年的发箭头的功能,那么你可以写一样的
If your environment supports ECMA Script 2015's Fat-arrow functions, then you can write the same as
_.sortBy(data, (obj) => obj.profile.data.investor_type);
这篇关于使用下划线到阵列中的嵌套对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!