我想簡化并丟失最后一部分。
所以,我有一個物件陣列,例如:
const data = [{
"company": "aaa",
"qty": 1,
"id": 1
}, {
"company": "bbb",
"qty": 14,
"id": 2
}, {
"company": "ccc",
"qty": 2,
"id": 3
}, {
"company": "ddd",
"qty": 5,
"id": 4
},
{
"company": "eee",
"qty": 55,
"id": 5
}
];
我需要添加基于百分比qty并總體創建一個新物件,其中僅包含百分比大于“X”(假設為 10)的公司的詳細資訊,其他公司合并為“其他公司”,如類似
{
"idCompany": 2,
"name": "bbb",
"percentage": 18.181818181818183,
"qty": 14
},{
"idCompany": 5,
"name": "eee",
"percentage": 71.42857142857143,
"qty": 55
},{
"idCompany": null,
"name": "others",
"percentage": 10.391 // 100 - 71.42xxx - 18.18
"qty": 8 // 1 2 5
},
所以,我的步驟:
- 獲取總數:
const total = data.reduce((i, el) => i el.qty, 0);
- 添加百分比以開始物件:
let rawData = data.map(el => {
return {
name: el.company,
qty: el.qty,
idCompany: el.id,
percentage: (el.qty/total)*100,
}
})
- 只獲取百分比大于 X(例如 10)的公司
let consolidateCompanies = rawData.filter(el => el.percentage > 10);
但...
現在,我怎樣才能獲得 OTHERS 公司并添加到consolidateCompanies物件中?還有更漂亮的代碼?
const data = [{
"company": "aaa",
"qty": 1,
"id": 1
}, {
"company": "bbb",
"qty": 14,
"id": 2
}, {
"company": "ccc",
"qty": 2,
"id": 3
}, {
"company": "ddd",
"qty": 5,
"id": 4
},
{
"company": "eee",
"qty": 55,
"id": 5
}
];
// Get the total
const total = data.reduce((i, el) => i el.qty, 0);
// Add percentage to the objects
let rawData = data.map(el => {
return {
name: el.company,
qty: el.qty,
idCompany: el.id,
percentage: (el.qty/total)*100,
}
})
// Get only companies that have percentage greater than 10
let consolidateCompanies = rawData.filter(el => el.percentage > 10);
console.log(consolidateCompanies);
// But I'm missing 1, 3 and 4
uj5u.com熱心網友回復:
你可以通過幾個Array.reduce()電話來做到這一點。
我們會按名稱或“其他”對公司進行分組,為其他添加百分比和數量。
const data = [{ "company": "aaa", "qty": 1, "id": 1 }, { "company": "bbb", "qty": 14, "id": 2 }, { "company": "ccc", "qty": 2, "id": 3 }, { "company": "ddd", "qty": 5, "id": 4 }, { "company": "eee", "qty": 55, "id": 5 } ];
const total = data.reduce((i, el) => i el.qty, 0);
const minPercentage = 10;
const consolidateCompanies = Object.values(data.reduce((acc, el) => {
const percentage = (el.qty/total)*100;
const name = (percentage >= minPercentage) ? el.company: 'others';
const id = (percentage >= minPercentage) ? el.id: null;
acc[name] = acc[name] || { name, percentage: 0, qty: 0, id };
acc[name].percentage = percentage;
acc[name].qty = el.qty;
return acc;
}, {}))
console.log(consolidateCompanies);
.as-console-wrapper { max-height: 100% !important; }
uj5u.com熱心網友回復:
others您可以僅在必要時分開并添加,而無需使用另一個物件進行分組。
const
data = [{ company: "aaa", qty: 1, id: 1 }, { company: "bbb", qty: 14, id: 2 }, { company: "ccc", qty: 2, id: 3 }, { company: "ddd", qty: 5, id: 4 }, { company: "eee", qty: 55, id: 5 }],
totalQty = data.reduce((t, { qty }) => t qty, 0),
others = { idCompany: null, name: "others", percentage: 0, qty: 0 },
result = data.reduce((r, { company: name, qty, id: idCompany }) => {
const percentage = qty * 100 / totalQty;
if (percentage >= 10) {
r.push({ idCompany, name, percentage, qty });
} else {
others.qty = qty;
others.percentage = others.qty * 100 / totalQty;
}
return r;
}, []);
if (others.qty) result.push(others);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
uj5u.com熱心網友回復:
這是您的代碼的重構
const data = [{
"company": "aaa",
"qty": 1,
"id": 1
}, {
"company": "bbb",
"qty": 14,
"id": 2
}, {
"company": "ccc",
"qty": 2,
"id": 3
}, {
"company": "ddd",
"qty": 5,
"id": 4
},
{
"company": "eee",
"qty": 55,
"id": 5
}
];
const total = data.reduce((i, el) => i el.qty, 0)
const startWith = [{
"idCompany": null,
"name": "others",
"percentage": 0,
"qty": 0
}]
let rawData = data.reduce((acc, value) => {
// console.log('value', value)
const withPercentage = {
name: value.company,
qty: value.qty,
idCompany: value.id,
percentage: (value.qty/total)*100,
}
if (withPercentage.percentage > 10) {
acc.push(withPercentage)
} else {
acc[0].qty = withPercentage.qty;
acc[0].percentage = withPercentage.percentage;
}
return acc;
}, startWith).filter(f => f.percentage !== 0 && f.qty !== 0);
console.log(rawData)
uj5u.com熱心網友回復:
使用 forEach 代替 map 和 filter,并根據您的需要回圈物件,如下所示。
const minPerc =10;
const data = [{
"company": "aaa",
"qty": 1,
"id": 1
}, {
"company": "bbb",
"qty": 14,
"id": 2
}, {
"company": "ccc",
"qty": 2,
"id": 3
}, {
"company": "ddd",
"qty": 5,
"id": 4
},
{
"company": "eee",
"qty": 55,
"id": 5
}
];
// Get the total
const total = data.reduce((i, el) => i el.qty, 0);
let consolidateCompanies = [];
let otherObj={"idCompany": null,
"name": "others",
"percentage": 0,
"qty": 0};
data.forEach(ele=>{
let perc=(ele.qty/total)*100;
if(perc>minPerc){
consolidateCompanies.push({...ele,...{percentage:perc}})
}else{
otherObj.qty =ele.qty; otherObj.percentage =perc
} });
consolidateCompanies.push(otherObj);
console.log(consolidateCompanies);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/494109.html
標籤:javascript
上一篇:將陣列陣列轉換為單個物件陣列
