3 回答
TA贡献1859条经验 获得超6个赞
您可以使用reducepromotionId 对促销进行分组,然后映射条目以获得最终结果
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)
TA贡献1830条经验 获得超3个赞
你应该像这样使用 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
}
]
}
要获得准确的输出,请添加这些行:
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
}
]
}
]
TA贡献1829条经验 获得超13个赞
这是我的答案
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)
添加回答
举报