JavaScript-对数组进行分组,然后根据长度对分组结果排序
这是阵列;
var dataArr = [
{
"permalink": /* link*/
"subreddit": "mac"
}, {
"permalink": /* link*/
"subreddit": "worldnews"
}, {
"permalink": /* link*/
"subreddit": "MushroomGrowers"
}, {
"permalink": /* link*/
"subreddit": "chrome"
}, {
"permalink": /* link*/
"subreddit": "onions"
}, {
"permalink": /* link*/
"subreddit": "onions"
}, {
"permalink": /* link*/
"subreddit": "SquaredCircle"
}.....
]
基于“ subreddit”键的分组似乎
。
简单
{
ArtisanVideos: [
{
"permalink": ' link ',
subreddit: "ArtisanVideos"
},
{
"permalink": ' link ',
subreddit: "ArtisanVideos"
},
{
"permalink": ' link ',
subreddit: "ArtisanVideos"
}
],
chrome: [
{
"permalink": ' link ',
subreddit: "chrome"
}
],
laravel: [
{
"permalink": ' link ',
subreddit: "laravel"
},
{
"permalink": ' link ',
subreddit: "laravel"
},
{
"permalink": ' link ',
subreddit: "laravel"
}
],
mac: [
{
"permalink": ' link ',
subreddit: "mac"
}
]
}
很 阵列的长度
this is the array ;
var dataArr = [
{
"permalink": /* link*/
"subreddit": "mac"
}, {
"permalink": /* link*/
"subreddit": "worldnews"
}, {
"permalink": /* link*/
"subreddit": "MushroomGrowers"
}, {
"permalink": /* link*/
"subreddit": "chrome"
}, {
"permalink": /* link*/
"subreddit": "onions"
}, {
"permalink": /* link*/
"subreddit": "onions"
}, {
"permalink": /* link*/
"subreddit": "SquaredCircle"
}.....
]
grouping seems simple enough based on the "subreddit" key using underscore
const grouped = _.groupBy( dataArr, 'subreddit' )
returns an object something like this
{
ArtisanVideos: [
{
"permalink": ' link ',
subreddit: "ArtisanVideos"
},
{
"permalink": ' link ',
subreddit: "ArtisanVideos"
},
{
"permalink": ' link ',
subreddit: "ArtisanVideos"
}
],
chrome: [
{
"permalink": ' link ',
subreddit: "chrome"
}
],
laravel: [
{
"permalink": ' link ',
subreddit: "laravel"
},
{
"permalink": ' link ',
subreddit: "laravel"
},
{
"permalink": ' link ',
subreddit: "laravel"
}
],
mac: [
{
"permalink": ' link ',
subreddit: "mac"
}
]
}
Now, how do I sort the grouped objects based on the length of the array
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从我上述评论...
问题
为了实现中间分组目标,一个人将
降低
给定的数据 - 结构为分组条目的对象( /Developer.mozilla.org/en-us/docs/web/javascript/Reference/global_objects/objects/object/entries“ rel =“ nofollow noreferrer”>entries
现在可以是
ped进入仍然是未排序的结果数组的对象,在最后一步中将是map
sort
由每个对象的唯一数组-Valuelength
- property或通过语言环境比较单个属性名称(键) )。原因之一也可以实现OP的最初目标,但不建议实现。一个人不应真正取决于对象的密钥插入顺序。
From my above comment ...
The next provided solution does exactly what already got proposed.
In order to achieve the intermediate grouping goal one would
reduce
the given data-structure into an object of grouped entries (each entry holding an array of some of the former data-array items.Each of the
entries
now can bemap
ped into an own object of the still unsorted result array which within a last step will besort
ed by either each object's sole array-valuelength
-property or by a locale comparison of the single property names (keys).One of cause can achieve the OP's original goal as well but it is not recommended. One should not really depend on an object's key insertion order.