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
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.