Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用下划线对数组中的嵌套对象进行排序_Javascript_Arrays_Sorting_Underscore.js - Fatal编程技术网

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

Javascript 使用下划线对数组中的嵌套对象进行排序,javascript,arrays,sorting,underscore.js,Javascript,Arrays,Sorting,Underscore.js,考虑以下扩展对象: 现在,这些对象中的每一个都被存储到一个数组中,并且很容易按名称、电子邮件、创建地址或其他任何内容进行排序。但是,如果我想对用户档案投资者类型感到恼火,那该怎么办呢。在图像中,您可以执行以下操作:data.profile.data.investor\u type以获取投资者类型 如何使用下划线按对象中的嵌套属性对这些对象的数组进行排序?您可以使用显示的确切代码 _.sortBy(data, function(obj) { return obj.profile.data.inv

考虑以下扩展对象:

现在,这些对象中的每一个都被存储到一个数组中,并且很容易按名称、电子邮件、创建地址或其他任何内容进行排序。但是,如果我想对用户档案投资者类型感到恼火,那该怎么办呢。在图像中,您可以执行以下操作:
data.profile.data.investor\u type
以获取投资者类型

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

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

_.sortBy(data, function(obj) { return obj.profile.data.investor_type; });
如果您的环境支持,那么您可以编写与

_.sortBy(data, (obj) => obj.profile.data.investor_type);

可能重复的no不是重复。我有点不明白在这种情况下,如何按投资者类型对对象数组进行排序?对于数组中的每个元素,使用相应的
investor\u type
进行比较。因此,对象数组将根据
investor\u类型
进行排序。在您的示例“profile.data.investor\u类型”中,您肯定需要读取u.sortBy函数,按照我指定的给定值对数组进行排序。要玩的小把戏:@zobidafly你的意思是说这个解决方案行不通吗?@thefourtheye:不,它行得通,我只是添加了一条评论和一个链接来检查它是否行得通。。。