根据值(本身就是数组)将数组收集到子数组桶中

我有一个 Javascript 对象数组,如下所示。


[

  {

    email: 'alex@test.com',

    fn: 'Alex',

    sn: 'McPherson',

    phone: '01233xxxxx',

    hours: '40',

    rate: '20',

    amount: '200',

    vat: '60',

    agency: 'test',

    start: '08/06/2017',

    end: '10/06/2017',

    shipping: {

      addresses: [

        {

          id: '1234',

          area: 'xzy'

        },

        {

          id: '2345',

          area: 'uhj'

        }

      ]

    }

  },

  {

    email: 'mike@test.com',

    fn: 'Mike',

    sn: 'Mann',

    phone: '01233xxxxx',

    hours: '50',

    rate: '70',

    amount: '500',

    vat: '90',

    agency: 'test',

    start: '08/06/2017',

    end: '10/06/2017',

    shipping: {

      addresses: [

        {

          id: '1234',

          area: 'xzy'

        },

        {

          id: '3456',

          area: 'uio'

        }

      ]

    }

  },

  {

    email: 'fred@test.com',

    fn: 'Fred',

    sn: 'Frogg',

    phone: '01233xxxxx',

    hours: '80',

    rate: '90',

    amount: '800',

    vat: '100',

    agency: 'test',

    start: '08/06/2017',

    end: '10/06/2017',

    shipping: {

      addresses: [

        {

          id: '4567',

          area: 'asdaf'

        },

        {

          id: '3456',

          area: 'uio'

        }

      ]

    }

  },

  {

    email: 'alex@test.com',

    fn: 'Alex',

    sn: 'McPherson',

    phone: '01233xxxxx',

    hours: '90',

    rate: '30',

    amount: '900',

    vat: '120',

    agency: 'test',

    start: '08/06/2017',

    end: '10/06/2017',

    shipping: {

      addresses: [

        {

          id: '4567',

          area: 'asdaf'

        },

        {

          id: '5678',

          area: 'asdf'

        }

      ]

    }

  }

]

我理想的情况是将那些具有相同值(shipping.addresses.id)的对象分组到自己的对象子数组中。预期结果。

我可以使用特定键(下面的代码)使用特定属性对输入数组进行分组,但我似乎无法根据数组本身的键来重新排序数组。


Array.from(

    data.reduce( 

        (acc, o) => (acc.get(o.email).push(o), acc),

        new Map(data.map( o => [o.email, []] ))

    ), ([key, value]) => value

)


开心每一天1111
浏览 137回答 1
1回答

慕桂英3389331

您可以data使用 as 键将数组缩减为一个对象shipping.addresses.id,并使用 返回一个数组Object.values()。您将需要迭代addresses每个对象的数组,并在遇到每个对象时为每个对象创建一个条目id,并推送到这些条目以获取具有相同id.const byAddressId = Object.values(  data.reduce((a, o) => {    o.shipping.addresses.forEach(({id, area}) => {      a[id]  = {...a[id] ?? {id: id, area: area, data: []}};      a[id]['data'].push({...o});    });    return a;    }, {}));const data = [{"email": "alex@test.com","fn": "Alex","sn": "McPherson","phone": "01233xxxxx","hours": "40","rate": "20","amount": "200","vat": "60","agency": "test","start": "08/06/2017","end": "10/06/2017","shipping": {   "addresses": [  { "id": "1234", "area": "xzy"  },  { "id": "2345", "area": "uhj"  }   ]}},{"email": "mike@test.com","fn": "Mike","sn": "Mann","phone": "01233xxxxx","hours": "50","rate": "70","amount": "500","vat": "90","agency": "test","start": "08/06/2017","end": "10/06/2017","shipping": {   "addresses": [  { "id": "1234", "area": "xzy"  },  { "id": "3456", "area": "uio"  }   ]}},{"email": "fred@test.com","fn": "Fred","sn": "Frogg","phone": "01233xxxxx","hours": "80","rate": "90","amount": "800","vat": "100","agency": "test","start": "08/06/2017","end": "10/06/2017","shipping": {   "addresses": [  { "id": "4567", "area": "asdaf"  },  { "id": "3456", "area": "uio"  }   ]}},{"email": "alex@test.com","fn": "Alex","sn": "McPherson","phone": "01233xxxxx","hours": "90","rate": "30","amount": "900","vat": "120","agency": "test","start": "08/06/2017","end": "10/06/2017","shipping": {   "addresses": [  { "id": "4567", "area": "asdaf"  },  { "id": "5678", "area": "asdf"  } ]}}];// return array of Object.values from the accumulatorconst byAddressId = Object.values(  // reduce the data array into an object with shipping.addresses.id as keys  data.reduce((a, o) => {    // iterate over all addresses for each element    o.shipping.addresses.forEach(({id, area}) => {      // check if an id entry exists, otherwise create one      a[id]  = {...a[id] ?? {id: id, area: area, data: []}};      // push the object to the data array of the id object      a[id]['data'].push({...o});    });    return a;    }, {}));console.log(byAddressId);话虽这么说,与问题中包含的示例map()相比,您可以使用相同的方法来节省两次调用。group by emailconst byEmail = Object.values(     data.reduce((a, o) => (a[o.email] = [...a[o.email] ?? [], {...o}], a), {}));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript