简体   繁体   English

首先,然后使用lodash根据名称对数组进行排序?

[英]groupBy first, then sort array based on name with lodash?

I have some json in the form of: 我有一些json形式:

data = { 
    "1": {"name": "eve", "type": "sergeant", "age": 22},
    "2": {"name": "bob", "type": "sergeant", "age": 33},
    "3": {"name": "bats", "type": "private", "age": 90},
    "4": {"name": "carol", "type": "captain", "age": 50},
    "5": {"name": "noogle", "type": "sergeant", "age": 34},
    "6": {"name": "good", "type": "sergeant", "age": 47},
    "7": {"name": "alice", "type": "private", "age": 34}
}

How can I groupBy their type , and then within each type, they are sorted by their name in ascending order? 如何groupBy它们的type groupBy ,然后在每种类型中名称将它们升序排序?

I tried to do it with: 我试图做到这一点:

let grouped = _.groupBy(Object.keys(data), function(id) {
    return data[id].type;
});

Which will give me something like: 这将给我类似的东西:

{
    "sergeant": ["1", "2", "5", "6"],
    "private": ["3", "7"],
    "captain": ["4"]
}

But when I try to sortBy it gives me the same object as above: 但是当我尝试sortBy它给了我与上面相同的对象:

_.map(Object.keys(grouped), function(type_name) {
    grouped[type_name] = _.sortedBy(grouped[type_name], function(id) {
        return data[id].name;
    });
});

How can I sort the array after I've grouped the objects together? 将对象分组在一起后如何排序数组? Is there a way I can cleanly chain groupBy and sortBy together instead of splitting logic like I have above? 有没有一种方法可以将groupBysortBy干净地链接在一起,而不是像上面那样拆分逻辑?

You can use the _.mapValues method to sort each group after you've grouped them up by using _.groupBy . 通过使用_.mapValues对每个组进行分组之后,可以使用_.mapValues方法对它们进行_.groupBy Just take each value and return the sorted version of it in your _.mapValues callback function. 只需获取每个值,然后在_.mapValues回调函数中返回其排序版本_.mapValues

Example: 例:

 data = { "1": {"name": "eve", "type": "sergeant", "age": 22}, "2": {"name": "bob", "type": "sergeant", "age": 33}, "3": {"name": "bats", "type": "private", "age": 90}, "4": {"name": "carol", "type": "captain", "age": 50}, "5": {"name": "noogle", "type": "sergeant", "age": 34}, "6": {"name": "good", "type": "sergeant", "age": 47}, "7": {"name": "alice", "type": "private", "age": 34} } console.log(_.mapValues(_.groupBy(data, "type"), v => _.sortBy(v, "name"))) 
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script> 

Or, if you just want grouped and sorted keys, or grouped and sorted key/values, you can use the same approach, although its not quite as easy on the eyes: 或者,如果只希望对键进行分组和排序,或者对键/值进行分组和排序,则可以使用相同的方法,尽管这种方法并不容易:

 data = { "1": {"name": "eve", "type": "sergeant", "age": 22}, "2": {"name": "bob", "type": "sergeant", "age": 33}, "3": {"name": "bats", "type": "private", "age": 90}, "4": {"name": "carol", "type": "captain", "age": 50}, "5": {"name": "noogle", "type": "sergeant", "age": 34}, "6": {"name": "good", "type": "sergeant", "age": 47}, "7": {"name": "alice", "type": "private", "age": 34} } // If all you want are grouped and sorted keys: console.log(_.mapValues(_.groupBy(_.keys(data), k => data[k].type), v => _.sortBy(v, i => data[i].name))) // Or for grouped and sorted keys and values: console.log(_.mapValues(_.groupBy(_.keys(data), k => data[k].type), v => _.sortBy(v, i => data[i].name).map(k => ({[k]: data[k]})))) 
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM