我有一個物件陣列(購物車),我想洗掉具有相同obj.id和obj.arr空陣列的重復項。之后增加obj.quantity一個。這是原始的arr:
const cart = [
{
"id": 1,
"name": "book",
"arr": [],
"quantity": 1
},
{
"id": 2,
"name": "pen",
"arr": ["asa", "asd"],
"quantity": 3
},
{
"id": 1,
"name": "book",
"arr": [],
"quantity": 1
},
{
"id": 3,
"name": "ball pen",
"arr": ["azx", "dcv"],
"quantity": 1
}
]
預期的輸出應該是這樣的:
const cart = [
{
"id": 1,
"name": "book",
"arr": [],
"quantity": 2
},
{
"id": 2,
"name": "pen",
"arr": ["asa", "asd"],
"quantity": 3
},
{
"id": 3,
"name": "ball pen",
"arr": ["azx", "dcv"],
"quantity": 1
}
]
我不知道object.quantity洗掉重復項后如何更新。如果可能的話,我想保留索引最小的專案。
請你幫助我好嗎?
謝謝
uj5u.com熱心網友回復:
為了保持訂單,我會
- 建立一個以 id 為鍵的映射和一個以物件為值的陣列
- 迭代此映射并檢查重復項(= 陣列中的多個物件)
- 檢查所有重復項上的空陣列欄位
- 在第一次出現時增加數量并覆寫現有條目(假設所有重復項具有相同的數量并且它應該只增加一個,即使有兩個以上的重復項)
- 重建購物車
const cart = [{
"id": 1,
"name": "book",
"arr": [],
"quantity": 1
}, {
"id": 2,
"name": "pen",
"arr": ["asa", "asd"],
"quantity": 3
}, {
"id": 1,
"name": "book",
"arr": [],
"quantity": 1
}, {
"id": 3,
"name": "ball pen",
"arr": ["azx", "dcv"],
"quantity": 1
}]
function removeDuplicates(cart) {
let map = new Map()
// groupBy id
cart.forEach(obj => {
let entry = map.has(obj.id)
if (entry) {
let value = map.get(obj.id)
map.set(obj.id, [...value, obj])
} else {
map.set(obj.id, [obj])
}
})
map.forEach((value, key) => {
// doubles?
if (value.length > 1) {
let allEmptyArrays = value.every(obj => obj.arr.length === 0)
if (allEmptyArrays) {
// keep only the first value
let keepValue = value[0]
// adjust quantity
keepValue.quantity
// overwrite existing entry
map.set(key, [keepValue])
}
}
})
// rebuild cart
return Array.from(map).map(([_, value]) => value[0])
}
console.log(removeDuplicates(cart))
uj5u.com熱心網友回復:
修改了如何從物件陣列中洗掉所有重復項的代碼?. 而不是filter我過去常常reduce查找并更新重復專案的數量
const cart = [
{
"id": 1,
"name": "book",
"arr": [],
"quantity": 1
},
{
"id": 2,
"name": "pen",
"arr": ["asa", "asd"],
"quantity": 3
},
{
"id": 1,
"name": "book",
"arr": [],
"quantity": 1
},
{
"id": 3,
"name": "ball pen",
"arr": ["azx", "dcv"],
"quantity": 1
}
]
function findDupUpdateQty(arr){
return arr.reduce((acc, currentVal, index, self) => {
if(index === self.findIndex((e)=> e.id === currentVal.id && e.arr.length === currentVal.arr.length)){
acc.push(currentVal)
} else {
//if there's a duplicate then update quanitity
acc[acc.findIndex((e)=> e.id === currentVal.id && e.arr.length === currentVal.arr.length)].quantity = currentVal.quantity
}
return acc
},[])
}
console.log(findDupUpdateQty(cart))
使用一些@Corrl 建議進行編輯:
const cart = [
{
"id": 1,
"name": "book",
"arr": [],
"quantity": 1
},
{
"id": 2,
"name": "pen",
"arr": ["asa", "asd"],
"quantity": 3
},
{
"id": 1,
"name": "book",
"arr": [],
"quantity": 1
},
{
"id": 3,
"name": "ball pen",
"arr": ["azx", "dcv"],
"quantity": 1
}
]
function findDupUpdateQty(arr){
return arr.reduce((acc, currentVal, index, self) => { //check if both arrays are the same
let firstFoundIndex = self.findIndex((e)=> e.id === currentVal.id && JSON.stringify(e.arr) === JSON.stringify(currentVal.arr))
if(index === firstFoundIndex){
acc.push(currentVal)
} else {
self[firstFoundIndex].quantity = currentVal.quantity
}
return acc
},[])
}
console.log(findDupUpdateQty(cart))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411918.html
標籤:
