Consider the following expanded object:
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?
You can use _.sortBy
with the exact code you have show
_.sortBy(data, function(obj) { return obj.profile.data.investor_type; });
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.