这可能是一种非常愚蠢的事情,我正在思考,或者根本就没有想到(已经醒了将近30个小时,所以很可能)。
我有一系列"销售",见下文:
let sales = [
{
amount: 100,
customerName: "Johan"
},
{
amount: 262,
customerName: "James"
},
{
amount: 42,
customerName: "Fraser"
},
{
amount: 983,
customerName: "Calum"
},
{
amount: 246,
customerName: "Johan"
}
,
{
amount: 873,
customerName: "James"
}
,
{
amount: 210,
customerName: "Fraser"
},
{
amount: 68,
customerName: "Calum"
}
];
我想要做的是循环数组而不是每个客户的2条记录,我想要1条记录,但总数应该是与该客户相加的所有相关金额。
我尝试使用sales.forEach
,然后将值推送到新数组......这只是再次创建了相同的数组..基本上。
我也尝试过使用sales.reduce
,但我的输出没有按照上面描述的那样做,相反它仍然有2条记录,除了第二条记录添加了总数。
我看了一下,甚至在这里,没有任何完全就像我正在寻找的那样。
我想在纯javascript中执行此操作。
如果您需要澄清,只需在评论中询问,而不是标记!
答案 0 :(得分:1)
有两种方法可以做到这一点,其中一种方法是使用reduce:
sales.reduce((newSales, item) => {
const existingItem = newSales.find(el => el.customerName === item.customerName);
// it exists, add to the existing one
if (existingItem) { existingItem.amount += item.amount; }
// it doesn't exist, add the one we have
else { newSales.push(item); }
return newSales;
}, []); // start with an empty array.
答案 1 :(得分:0)
您可以尝试这样的事情:
var sales = [
{ amount: 100, customerName: "Johan"},
{ amount: 262, customerName: "James"},
{ amount: 42, customerName: "Fraser"},
{ amount: 983, customerName: "Calum"},
{ amount: 246, customerName: "Johan"},
{ amount: 873, customerName: "James"},
{ amount: 210, customerName: "Fraser"},
{ amount: 68, customerName: "Calum"}];
var _tmp = {};
// Sort array using customer name
sales.sort(function(a,b){
if(a.customerName < b.customerName) return -1;
if(a.customerName > b.customerName) return 1;
return 0;
})
// Find and remove duplicate entries
.forEach(function(item){
_tmp[item.customerName] = (_tmp[item.customerName] || 0) + item.amount;
});
// Create final result
var result = [];
Object.keys(_tmp).forEach(function(key){
result.push({"customerName":key, "amount": _tmp[key]});
});
document.write("<pre>" + JSON.stringify(result,0,4) + "</pre>")
&#13;
答案 2 :(得分:0)
var sales = [{
amount: 100,
customerName: "Johan"
}, {
amount: 262,
customerName: "James"
}, {
amount: 42,
customerName: "Fraser"
}, {
amount: 983,
customerName: "Calum"
}, {
amount: 246,
customerName: "Johan"
}, {
amount: 873,
customerName: "James"
}, {
amount: 210,
customerName: "Fraser"
}, {
amount: 68,
customerName: "Calum"
}];
var reduced_sales = sales
.reduce(function (prev, curr) {
var prev_amount = prev[curr.customerName] || 0
prev[curr.customerName] = prev_amount + curr.amount
return prev;
}, {});
var sales_final = [];
for (var prop in reduced_sales) {
sales_final.push({
amount: reduced_sales[prop],
customerName: prop
}
);
}
console.log(sales_final);
答案 3 :(得分:0)
您可以使用for..in
循环,Array.prototype.forEach()
var names = [], amounts = [], res = [];
for (var prop in sales) {
var n = sales[prop].customerName;
var a = sales[prop].amount;
var index = names.indexOf(n);
if (index == -1) {
names.push(n);
amounts.push(a);
} else {
amounts[index] += a;
}
};
names.forEach(function(name, key) {
// populate `res` array with results contained in `names`, `amounts`
res[key] = {customerName:name, amount:amounts[key]}
});
答案 4 :(得分:0)
也许最简单的方法是转换为地图
var salesByCustomerName = {};
for (var i = 0; i < sales.length; i++)
{
var sale = sales[i], customerName = sale.customerName;
salesByCustomerName[customerName] =
(salesByCustomerName[customerName] || 0) + sale.amount;
}
// Firebug prints: Object { Johan: 346, James: 1135, Fraser: 252, Calum: 1051 }
console.log(salesByCustomerName);