Lodash groupby字母

时间:2018-06-03 08:58:53

标签: javascript lodash

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

[
    {
      "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"
    }
]

现在我想要的是使用Lodash基于last_name prop创建另一个包含字母的主对象。

在这种情况下:

[
    {
      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"
        }
      ]
    }
  ]

3 个答案:

答案 0 :(得分:4)

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

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);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

答案 1 :(得分:0)

您可以非常轻松地与reduce分组,不需要库,只需选择last_name,推送到由第一个字母索引的对象中的数组,然后获取对象&#39; s值:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

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

var result = _(data)
  .groupBy('last_name[0]')
  .map((contacts, letter) => ({ letter, contacts }))
  .value();

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);
.as-console-wrapper{min-height:100%;top:0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>