循环对象文本数组并匹配客户

Looping an array of object literals and matching customers

本文关键字:客户 数组 对象 文本 循环      更新时间:2023-09-26

这可能是我过度思考或根本没有想到的非常愚蠢的事情(醒了将近 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);