我的人,我需要幫助,我不知道如何處理物件以使用香草獲得我需要的東西。我需要為每個“elem”按“型別”分組,并對價格和折扣值求和,并將它們回傳到一個陣列中。如果有人能向我解釋如何進行轉型,我將不勝感激。
我有這個物件
{
"w_elem": [
{
"elem": [
{"type": "type_a","price": "100","discount": "10"},
{"type": "type_b","price": "120","discount": "12"},
{"type": "type_a","price": "165","discount": "10"}
],
"user": "1001",
"userType": "A"
},
{
"elem": [
{"type": "type_a","price": "50","discount": "5"},
{"type": "type_b","price": "15","discount": "0"}
],
"user": "1011",
"userType": "B"
},
{
"elem": [
{"type": "type_a","price": "20","discount": "5"},
{"type": "type_b","price": "15","discount": "0"}
],
"user": "1011",
"userType": "B"
}
]
}
我必須回傳以下陣列
[
{"type": "type_a","price": "335","discount":"30"},
{"type":"type_b","price": "150","discount": "12"}
]
我嘗試了以下但我無法理解。
let helper = {};
const data = newArr.reduce((acc, currElem) => {
currElem.elem.forEach(item => {
if(!helper[currElem.type]) {
helper[currElem.type] = Object.assign({}, currElem);
acc.push(helper[currElem.type])
}else{
helper[currElem.type].price = currElem.price;
helper[currElem.type].discount = currElem.discount;
}
})
return acc;
}, [])
uj5u.com熱心網友回復:
下面的代碼片段應該可以解決問題。
至于解釋:
- 您想獲取所有型別物件的串列。您可以使用 flatMap 來做到這一點。
- 獲取型別的所有唯一值。在 ES6 中,您可以使用 SET 獲取串列中的所有 unqiue 值
- 然后,您可以使用過濾器來獲取具有相同型別的所有物件,然后僅對這些物件使用 reduce。
注意:由于 count 不存在,您需要為其定義一個初始值,并且由于您定義了 count,您還必須定義您對其執行 reduce 操作的其他值。(不需要 id ,因為你直接分配它并且之前的值無關緊要)
我希望你能按照我的簡短解釋。
const newJson = {
"w_elem": [
{
"elem": [
{"type": "type_a","price": "100","discount": "10"},
{"type": "type_b","price": "120","discount": "12"},
{"type": "type_a","price": "165","discount": "10"}
],
"user": "1001",
"userType": "A"
},
{
"elem": [
{"type": "type_a","price": "96.2","discount": "5"},
{"type": "type_b","price": "15","discount": "0"}
],
"user": "1011",
"userType": "B"
},
{
"elem": [
{"type": "type_a","price": "96.2","discount": "5"},
{"type": "type_b","price": "15","discount": "0"}
],
"user": "1011",
"userType": "B"
}
]
}
const newArr = newJson["w_elem"]
/* Get all elem */
const flatElem = newArr.flatMap((item) => {
return item["elem"]
})
const keys = [... new Set(flatElem.map(item => { return item["type"]}))]
let data = keys.map( id => {
return flatElem.filter( obj => obj["type"] === id )
.reduce((acc, cur) => (
{
type: id,
count: acc.count 1,
price: acc.price parseFloat(cur.price),
discount: acc.discount parseFloat(cur.discount)
}
), {count: 0, price: 0, discount: 0})
})
console.log(data)
uj5u.com熱心網友回復:
您可以通過制作平面元素陣列來簡化任務。然后按 對匯總值進行分組type。
const data = {"w_elem":[{"elem":[{"type":"type_a","price":"100","discount":"10"},{"type":"type_b","price":"120","discount":"12"},{"type":"type_a","price":"165","discount":"10"}],"user":"1001","userType":"A"},{"elem":[{"type":"type_a","price":"96.2","discount":"5"},{"type":"type_b","price":"15","discount":"0"}],"user":"1011","userType":"B"},{"elem":[{"type":"type_a","price":"96.2","discount":"5"},{"type":"type_b","price":"15","discount":"0"}],"user":"1011","userType":"B"}]};
const elems = data.w_elem.flatMap(({ elem }) => elem);
const result = Object.values(elems.reduce((acc, { type, price, discount }) => {
acc[type] ??= { type, count: 0, price: 0, discount: 0 };
acc[type].count = 1;
acc[type].price = Number(price);
acc[type].discount = Number(discount);
return acc;
}, {}));
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/449315.html
標籤:javascript 数组 json 减少
