循环对象文本数组并匹配客户
Looping an array of object literals and matching customers
这可能是我过度思考或根本没有想到的非常愚蠢的事情(醒了将近 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中做到这一点。
如果您需要澄清,只需在评论中询问,而不是标记!
有几种方法可以做到这一点,这里有一种使用 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.
你可以尝试这样的事情:
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>")
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);
您可以使用
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]}
});
也许最简单的方法是转换为地图
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);
相关文章:
- 如何从客户端的数组中获取用户输入和返回值
- 使用javascript数组的客户端网格
- 将列表<字符串>转换为客户端 ASP.NET MVC 上的 Json 数组
- 如何在node.js中将数组从服务器端传输到客户端
- 将服务器端 JavaScript 数组传递给客户端
- 使用Google应用程序脚本将数组传递给客户端函数
- 在通过套接字发送到客户端之前压缩中等大小的javascript数组是否值得
- 从客户端的对象数组中获取最新日期的优雅方法是什么
- 当将从javascript客户端调用javaapi时,在javaapi中使用2d数组
- 注册客户端脚本不适用于设置数组值
- jQuery 客户端分页在关联多维数组上
- 将数组从 websocket 发送到客户端
- 将数组从 Express 传递到 Jade 客户端 JavaScript
- 如何向客户端显示临时数组数据
- 如何在客户端的nodejs中评估var数组
- dojo/使用NodeJS请求一个png图像数组缓冲区,并将图像返回给客户端
- 通过readStream将数组的值与javascript文件一起发送到客户端
- 在客户端上缓存JSON数组
- 控制器中的ASP.NET MVC数组到客户端数组
- 通过JSON文件创建大型客户端Javascript对象数组的替代方案