首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lodash按字母分组

问Lodash按字母分组
EN

Stack Overflow用户
提问于 2018-06-03 16:58:53
回答 3查看 1.4K关注 0票数 0

我正在处理一个包含一些用户详细信息的对象,我需要将此对象转换为另一个对象来构建联系人应用程序。我使用Lodash来处理数组和对象。

代码语言:javascript
复制
[
    {
      "id": 1,
      "first_name": "Alie",
      "last_name": "Stigger",
      "email": "astigger0@lulu.com",
      "gender": "Female",
      "avatar":
        "https://robohash.org/sintnihiladipisci.jpg?size=200x200&set=set1",
      "phone_number": "617-845-6906"
    },
    {
      "id": 2,
      "first_name": "Kendall",
      "last_name": "Mayes",
      "email": "kmayes1@bbb.org",
      "gender": "Male",
      "avatar": "https://robohash.org/nemoetqui.jpg?size=200x200&set=set1",
      "phone_number": "193-270-2893"
    },
    {
      "id": 3,
      "first_name": "Yolanthe",
      "last_name": "Maddaford",
      "email": "ymaddaford2@reference.com",
      "gender": "Female",
      "avatar":
        "https://robohash.org/exercitationemestaccusamus.jpg?size=200x200&set=set1",
      "phone_number": "267-365-2165"
    }
]

现在所有我想要的是建立另一个对象从主要的一个,其中包含字母表字母的基础上使用last_name属性。

在这种情况下:

代码语言:javascript
复制
[
    {
      letter: "S",
      contacts: [
        {
          id: 1,
          first_name: "Alie",
          last_name: "Stigger",
          email: "astigger0@lulu.com",
          gender: "Female",
          avatar:
            "https://robohash.org/sintnihiladipisci.jpg?size=200x200&set=set1",
          phone_number: "617-845-6906"
        }
      ]
    },
    {
      letter: "M",
      contacts: [
        {
          id: 2,
          first_name: "Kendall",
          last_name: "Mayes",
          email: "kmayes1@bbb.org",
          gender: "Male",
          avatar: "https://robohash.org/nemoetqui.jpg?size=200x200&set=set1",
          phone_number: "193-270-2893"
        },
        {
          id: 3,
          first_name: "Yolanthe",
          last_name: "Maddaford",
          email: "ymaddaford2@reference.com",
          gender: "Female",
          avatar:
            "https://robohash.org/exercitationemestaccusamus.jpg?size=200x200&set=set1",
          phone_number: "267-365-2165"
        }
      ]
    }
  ]
EN

回答 3

Stack Overflow用户

发布于 2018-06-03 17:09:37

您可以按大写字母值分组,然后映射结果。

​

代码语言:javascript
复制
var data = [{ id: 1, first_name: "Alie", last_name: "Stigger", email: "astigger0@lulu.com", gender: "Female", avatar: "https://robohash.org/sintnihiladipisci.jpg?size=200x200&set=set1", phone_number: "617-845-6906" }, { id: 2, first_name: "Kendall", last_name: "Mayes", email: "kmayes1@bbb.org", gender: "Male", avatar: "https://robohash.org/nemoetqui.jpg?size=200x200&set=set1", phone_number: "193-270-2893" }, { id: 3, first_name: "Yolanthe", last_name: "Maddaford", email: "ymaddaford2@reference.com", gender: "Female", avatar: "https://robohash.org/exercitationemestaccusamus.jpg?size=200x200&set=set1", phone_number: "267-365-2165" }],
    result = _(data)
        .groupBy(o => o.last_name[0].toUpperCase())
        .map((contacts, letter) => ({ letter, contacts }))
        .value();

    console.log(result);
代码语言:javascript
复制
.as-console-wrapper { max-height: 100% !important; top: 0; }
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

​

票数 6
EN

Stack Overflow用户

发布于 2018-06-03 17:08:23

您可以使用lodash#groupBy按last_name的第一个字母对每个项目进行分组,使用lodash#map将分组的对象转换为转换后的数组。

代码语言:javascript
复制
var result = _(data)
  .groupBy('last_name[0]')
  .map((contacts, letter) => ({ letter, contacts }))
  .value();

​

代码语言:javascript
复制
var data = [
    {
      "id": 1,
      "first_name": "Alie",
      "last_name": "Stigger",
      "email": "astigger0@lulu.com",
      "gender": "Female",
      "avatar":
        "https://robohash.org/sintnihiladipisci.jpg?size=200x200&set=set1",
      "phone_number": "617-845-6906"
    },
    {
      "id": 2,
      "first_name": "Kendall",
      "last_name": "Mayes",
      "email": "kmayes1@bbb.org",
      "gender": "Male",
      "avatar": "https://robohash.org/nemoetqui.jpg?size=200x200&set=set1",
      "phone_number": "193-270-2893"
    },
    {
      "id": 3,
      "first_name": "Yolanthe",
      "last_name": "Maddaford",
      "email": "ymaddaford2@reference.com",
      "gender": "Female",
      "avatar":
        "https://robohash.org/exercitationemestaccusamus.jpg?size=200x200&set=set1",
      "phone_number": "267-365-2165"
    }
];

var result = _(data)
  .groupBy('last_name[0]')
  .map((contacts, letter) => ({ letter, contacts }))
  .value();
  
console.log(result);
代码语言:javascript
复制
.as-console-wrapper{min-height:100%;top:0}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

​

票数 2
EN

Stack Overflow用户

发布于 2018-06-03 17:04:08

您可以很容易地使用reduce进行分组,不需要库,只需选择last_name,推送到按第一个字母索引的对象中的数组,然后获取对象的值:

​

代码语言:javascript
复制
const input=[{"id":1,"first_name":"Alie","last_name":"Stigger","email":"astigger0@lulu.com","gender":"Female","avatar":"https://robohash.org/sintnihiladipisci.jpg?size=200x200&set=set1","phone_number":"617-845-6906"},{"id":2,"first_name":"Kendall","last_name":"Mayes","email":"kmayes1@bbb.org","gender":"Male","avatar":"https://robohash.org/nemoetqui.jpg?size=200x200&set=set1","phone_number":"193-270-2893"},{"id":3,"first_name":"Yolanthe","last_name":"Maddaford","email":"ymaddaford2@reference.com","gender":"Female","avatar":"https://robohash.org/exercitationemestaccusamus.jpg?size=200x200&set=set1","phone_number":"267-365-2165"}]

const output = Object.values(
  input.reduce((a, item) => {
    const letter = item.last_name[0];
    if (!a[letter]) a[letter] = [];
    a[letter].push(item);
    return a;
  }, {})
);
console.log(output);

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50664773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档