第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

開心每一天1111 2023-08-24 15:33:29
我有一個(gè) Javascript 對(duì)象數(shù)組,如下所示。[  {    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)的對(duì)象分組到自己的對(duì)象子數(shù)組中。預(yù)期結(jié)果。我可以使用特定鍵(下面的代碼)使用特定屬性對(duì)輸入數(shù)組進(jìn)行分組,但我似乎無法根據(jù)數(shù)組本身的鍵來重新排序數(shù)組。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貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以data使用 as 鍵將數(shù)組縮減為一個(gè)對(duì)象shipping.addresses.id,并使用 返回一個(gè)數(shù)組Object.values()。您將需要迭代addresses每個(gè)對(duì)象的數(shù)組,并在遇到每個(gè)對(duì)象時(shí)為每個(gè)對(duì)象創(chuàng)建一個(gè)條目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 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()相比,您可以使用相同的方法來節(jié)省兩次調(diào)用。group by email

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


查看完整回答
反對(duì) 回復(fù) 2023-08-24
  • 1 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)