亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據值(本身就是數組)將數組收集到子數組桶中

根據值(本身就是數組)將數組收集到子數組桶中

開心每一天1111 2023-08-24 15:33:29
我有一個 Javascript 對象數組,如下所示。[  {    email: '[email protected]',    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: '[email protected]',    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: '[email protected]',    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: '[email protected]',    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)
查看完整描述

1 回答

?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

您可以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": "[email protected]","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": "[email protected]","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": "[email protected]","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": "[email protected]","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 accumulator

const 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 email

const byEmail = Object.values(
    data.reduce((a, o) => (a[o.email] = [...a[o.email] ?? [], {...o}], a), {}));


查看完整回答
反對 回復 2023-08-24
  • 1 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號