const data = [
{
productId: 6,
productName: "pouch",
productPrice: 29000,
discountRate: 19,
optionName: "13inch",
optionPrice: 0,
qty: 2,
},
{
productId: 6,
productName: "pouch",
productPrice: 29000,
discountRate: 19,
optionName: "15inch",
optionPrice: 1000,
qty: 1,
},
]
const results = data.reduce((prev, curr) => {
if (prev.productId === curr.productId) {
???
}
}, []);
輸出
{
productId: 6,
productName: "pouch",
productPrice: 29000,
discountRate: 19,
option: [
{ optionName: "13inch", optionPrice: 0, qty: 2 },
{ optionName: "15inch", optionPrice: 1000, qty: 1 },
],
},
如果productId和上面的代碼一樣,
我想洗掉key:value的重疊部分,為非重疊部分創建一個選項物件,并為value創建一個陣列物件。
我試圖通過使用reduce來解決它,但它并不順利,所以我問你一個問題。
uj5u.com熱心網友回復:
一種方法是將陣列轉換為物件,并productId用作鍵:
const data = [
{
productId: 6,
productName: "pouch",
productPrice: 29000,
discountRate: 19,
optionName: "13inch",
optionPrice: 0,
qty: 2,
},
{
productId: 6,
productName: "pouch",
productPrice: 29000,
discountRate: 19,
optionName: "15inch",
optionPrice: 1000,
qty: 1,
},
{
productId: 4,
productName: "pouch",
productPrice: 29000,
discountRate: 19,
optionName: "15inch",
optionPrice: 1000,
qty: 1,
},
]
const results = Object.values(data.reduce((prev, curr) => {
if (prev[curr.productId])
{
const obj = prev[curr.productId];
if (!obj.option)
{
obj.option = [];
obj.option.push({optionName: obj.optionName, optionPrice: obj.optionPrice, qty: obj.qty});
delete obj.optionName;
delete obj.optionPrice;
delete obj.qty;
}
prev[curr.productId].option.push({optionName: curr.optionName, optionPrice: curr.optionPrice, qty: curr.qty});
}
else
prev[curr.productId] = Object.assign({}, curr);
return prev;
}, {}));
console.log(results);
uj5u.com熱心網友回復:
這是一種純粹的功能性方法,不會改變現有陣列資料的任何部分:
function deduplicate (array) {
const results = [];
const idCache = new Set();
for (const item of array) {
// skip if we've already seen products with the same ID
if (idCache.has(item.productId)) continue;
idCache.add(item.productId);
// get products with matching ID
const items = array.filter(({productId}) => productId === item.productId);
// if there were none, store the single product as-is and continue
if (items.length === 0) {
results.push({...item});
continue;
}
// add initial product to others, preserving order
items.unshift(item);
const merged = {};
for (const key of Object.keys(item)) {
const values = items.map(item => item[key]);
// if all the same value, set it on the merged object
if (values.every(val => val === item[key])) merged[key] = item[key];
else {
// set to property on obj in "options" array
for (const [idx, val] of values.entries()) {
((merged.options ??= [])[idx] ??= {})[key] = val;
}
}
}
results.push(merged);
}
return results;
}
const data = [
{
productId: 6,
productName: "pouch",
productPrice: 29000,
discountRate: 19,
optionName: "13inch",
optionPrice: 0,
qty: 2,
},
{
productId: 6,
productName: "pouch",
productPrice: 29000,
discountRate: 19,
optionName: "15inch",
optionPrice: 1000,
qty: 1,
},
{
productId: 5,
productName: "pouch",
productPrice: 29000,
discountRate: 19,
optionName: "15inch",
optionPrice: 1000,
qty: 1,
},
];
const result = deduplicate(data);
console.log(result);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474614.html
標籤:javascript 数组 目的 减少
上一篇:迭代物件并替換值
下一篇:將函式應用于多維陣列的每個元素
