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

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

如何按數(shù)組對象分組并使用值鍵javascript創(chuàng)建數(shù)組

如何按數(shù)組對象分組并使用值鍵javascript創(chuàng)建數(shù)組

狐的傳說 2023-04-14 16:22:00
如何根據(jù)值對數(shù)組對象進(jìn)行分組以創(chuàng)建新的值數(shù)組對象輸入var promotions = [    {        "promotionID": 2,        "id": 1,        "qty": 1,        "productID": 1,        "product": "Nu Milk Tea 330ml",        "operator": null    }, {        "promotionID": 2,        "id": 2,        "qty": 2,        "productID": 2,        "product": "Product testing 2",        "operator": 1    }, {        "promotionID": 2,        "id": 3,        "qty": 3,        "productID": 3,        "product": "Golda Coffee Dolce Latte 200ml",        "operator": 2    }, {        "promotionID": 3,        "id": 4,        "qty": 8,        "productID": 54,        "product": "Masker Skrineer 3ply Motif 5pcs",        "operator": null    }, {        "promotionID": 3,        "id": 5,        "qty": 5,        "productID": 53,        "product": "Masker Skrineer 1ply Grey 5pcs",        "operator": 2    }, {        "promotionID": 3,        "id": 6,        "qty": 5,        "productID": 52,        "product": "Oronamin C Drink 120ml",        "operator": 1    }]我想制作一個(gè)新的汽車對象數(shù)組,這些對象按以下方式分組promotionID預(yù)期產(chǎn)出[    {        "promotionID": 2,        "data" : [            {                "promotionID": 2,                "id": 1,                "qty": 1,                "productID": 1,                "product": "Nu Milk Tea 330ml",                "operator": null            }, {                "promotionID": 2,                "id": 2,                "qty": 2,                "productID": 2,                "product": "Product testing 2",                "operator": 1            }, {                "promotionID": 2,                "id": 3,                "qty": 3,                "productID": 3,                "product": "Golda Coffee Dolce Latte 200ml",                "operator": 2            }         ]    },]
查看完整描述

3 回答

?
BIG陽

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊

您可以使用reducepromotionId 對促銷進(jìn)行分組,然后映射條目以獲得最終結(jié)果


var promotions = [

  {

    promotionID: 2,

    id: 1,

    qty: 1,

    productID: 1,

    product: 'Nu Milk Tea 330ml',

    operator: null

  },

  {

    promotionID: 2,

    id: 2,

    qty: 2,

    productID: 2,

    product: 'Product testing 2',

    operator: 1

  },

  {

    promotionID: 2,

    id: 3,

    qty: 3,

    productID: 3,

    product: 'Golda Coffee Dolce Latte 200ml',

    operator: 2

  },

  {

    promotionID: 3,

    id: 4,

    qty: 8,

    productID: 54,

    product: 'Masker Skrineer 3ply Motif 5pcs',

    operator: null

  },

  {

    promotionID: 3,

    id: 5,

    qty: 5,

    productID: 53,

    product: 'Masker Skrineer 1ply Grey 5pcs',

    operator: 2

  },

  {

    promotionID: 3,

    id: 6,

    qty: 5,

    productID: 52,

    product: 'Oronamin C Drink 120ml',

    operator: 1

  }

]


const res = Array.from(

  promotions

    .reduce((acc, promotion) => {

      acc.set(

        promotion.promotionID,

        (acc.get(promotion.promotionID) || []).concat(promotion)

      )

      return acc

    }, new Map)

    .entries(),

  ([promotionId, data]) => ({ promotionId, data })

)


console.log(res)


查看完整回答
反對 回復(fù) 2023-04-14
?
牛魔王的故事

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊

你應(yīng)該像這樣使用 lodash。


const _ = require('lodash');

let filteredPromotions=_.groupBy(promotions,'promotionID');

輸出:


{

  '2': [

    {

      promotionID: 2,

      id: 1,

      qty: 1,

      productID: 1,

      product: 'Nu Milk Tea 330ml',

      operator: null

    },

    {

      promotionID: 2,

      id: 2,

      qty: 2,

      productID: 2,

      product: 'Product testing 2',

      operator: 1

    },

    {

      promotionID: 2,

      id: 3,

      qty: 3,

      productID: 3,

      product: 'Golda Coffee Dolce Latte 200ml',

      operator: 2

    }

  ],

  '3': [

    {

      promotionID: 3,

      id: 4,

      qty: 8,

      productID: 54,

      product: 'Masker Skrineer 3ply Motif 5pcs',

      operator: null

    },

    {

      promotionID: 3,

      id: 5,

      qty: 5,

      productID: 53,

      product: 'Masker Skrineer 1ply Grey 5pcs',

      operator: 2

    },

    {

      promotionID: 3,

      id: 6,

      qty: 5,

      productID: 52,

      product: 'Oronamin C Drink 120ml',

      operator: 1

    }

  ]

}

要獲得準(zhǔn)確的輸出,請?zhí)砑舆@些行:


    filteredPropomotions = Object.keys(filteredPropomotions).map((key, index)=> {return{promotionID:key,data:filteredPropomotions[key]}}

);

輸出:


[

    {

        "promotionID": 2,

        "data" : [

            {

                "promotionID": 2,

                "id": 1,

                "qty": 1,

                "productID": 1,

                "product": "Nu Milk Tea 330ml",

                "operator": null

            }, {

                "promotionID": 2,

                "id": 2,

                "qty": 2,

                "productID": 2,

                "product": "Product testing 2",

                "operator": 1

            }, {

                "promotionID": 2,

                "id": 3,

                "qty": 3,

                "productID": 3,

                "product": "Golda Coffee Dolce Latte 200ml",

                "operator": 2

            } 

        ]

    },

    {

        "promotionID": 3,

        "data" : [

            {

                "promotionID": 3,

                "id": 4,

                "qty": 8,

                "productID": 54,

                "product": "Masker Skrineer 3ply Motif 5pcs",

                "operator": null

            }, {

                "promotionID": 3,

                "id": 5,

                "qty": 5,

                "productID": 53,

                "product": "Masker Skrineer 1ply Grey 5pcs",

                "operator": 2

            }, {

                "promotionID": 3,

                "id": 6,

                "qty": 5,

                "productID": 52,

                "product": "Oronamin C Drink 120ml",

                "operator": 1

            }

        ]

    }

 

]


查看完整回答
反對 回復(fù) 2023-04-14
?
烙印99

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊

這是我的答案


var promotions = [

  {

    promotionID: 2,

    id: 1,

    qty: 1,

    productID: 1,

    product: "Nu Milk Tea 330ml",

    operator: null,

  },

  {

    promotionID: 2,

    id: 2,

    qty: 2,

    productID: 2,

    product: "Product testing 2",

    operator: 1,

  },

  {

    promotionID: 2,

    id: 3,

    qty: 3,

    productID: 3,

    product: "Golda Coffee Dolce Latte 200ml",

    operator: 2,

  },

  {

    promotionID: 3,

    id: 4,

    qty: 8,

    productID: 54,

    product: "Masker Skrineer 3ply Motif 5pcs",

    operator: null,

  },

  {

    promotionID: 3,

    id: 5,

    qty: 5,

    productID: 53,

    product: "Masker Skrineer 1ply Grey 5pcs",

    operator: 2,

  },

  {

    promotionID: 3,

    id: 6,

    qty: 5,

    productID: 52,

    product: "Oronamin C Drink 120ml",

    operator: 1,

  },

];


const objectPromotion = {};


for (index in promotions) {

  const dataPromotion = objectPromotion[promotions[index].promotionID];

  console.log("dataPromotion", dataPromotion)

  if (dataPromotion) {

    dataPromotion.push(promotions[index]);

  } else {

    objectPromotion[promotions[index].promotionID] = [promotions[index]];

  }

}


const result = Object.keys(objectPromotion).map((item) => {

  return {

    promotionID: item,

    data: objectPromotion[item],

  };

});


console.log("result", result)


查看完整回答
反對 回復(fù) 2023-04-14
  • 3 回答
  • 0 關(guān)注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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