简体   繁体   English

使用 lodash 对一组对象进行排序:不起作用

[英]Sorting an array of objects with lodash: not working

I have an array that looks like this one (it has more objects, but the structure is the same):我有一个看起来像这样的数组(它有更多的对象,但结构是相同的):

[
  {
      especiality: "surgery",
      users: [
          {
              id: "182",
              country: "Colombia",
              province: "Bogota",
              telephone: "211112212",
              neighbourhood: "La Santa"
              region: "South",
          },
          {
            id: "182",
            country: "Venezuela",
            province: "Caracas",
            telephone: "322323333",
            region: "North",
        },
        {
          id: "183",
          country: "Brasil",
          telephone: "23232333",
          neighbourhood: "Santos"
          region: "South",
      },
      ]
  },

I want the addresses, if the ID is the same, to compose one single array (I need to map these elements).如果 ID 相同,我希望地址组成一个数组(我需要映射这些元素)。 The outlook should look like this one:前景应该是这样的:

user: [{id: 182, locations[(if they exist)               
              country: "Colombia",
              province: "Bogota",
              telephone: "211112212",
              neighbourhood: "La Santa"
              region: "South"], [country: "Venezuela",
            province: "Caracas",
            telephone: "322323333",
            region: "North"],}]

I´m currently trying this, but it´s not working at all:我目前正在尝试这个,但它根本不起作用:

getGroups = test => {
  _.chain(test)
  .groupBy("id")
  .toPairs()
  .map(item => _.zipObject(["id", "country", "province", "neighbourhood", "region"], item))
  .value();
  return test
  }

What am I doing wrong and how can I account for values that may not be available in all objects?我做错了什么,我如何解释可能并非在所有对象中都可用的值?

After grouping the items by the id , map the groups, and create an object with the id , and the items of the group as locations .id对项目进行分组后,映射组,并创建一个以id为对象的对象,该组的项目为locations Map the locations, and use _.omit() to remove the id from them.映射位置,并使用_.omit()从中删除id

I'm not sure about how you want to handle the outer array.我不确定您想如何处理外部数组。 I've used _.flatMap() to get a single array of users, but there's a commented option if you need to maintain the original structure.我使用_.flatMap()来获取单个用户数组,但是如果您需要维护原始结构,则有一个注释选项。

 getGroups = test => _(test) .groupBy("id") .map((locations, id) => ({ id, locations: _.map(locations, l => _.omit(l, 'id')) })) .value(); const data = [{"especiality":"surgery","users":[{"id":"182","country":"Colombia","province":"Bogota","telephone":"211112212","neighbourhood":"La Santa","region":"South"},{"id":"182","country":"Venezuela","province":"Caracas","telephone":"322323333","region":"North"},{"id":"183","country":"Brasil","telephone":"23232333","neighbourhood":"Santos","region":"South"}]}]; const result = _.flatMap(data, ({ users }) => getGroups(users)); /** maintains the original structure const result = _.map(data, ({ users, ...rest }) => ({ ...rest, users: getGroups(users) })); **/ console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

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

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