JavaScript 试图找到特定分支机构的最高销售额

JavaScript trying to find the highest sales made in a particular branch

我正在尝试创建一个 class 以找出在 revenue.I 中取得最大收益的分支相信,我需要对多天的所有分支求和并确定哪个分支的整体销售额最高.但我有点迷路了。有人能帮助我吗?感谢帮助

class SalesItem {
    constructor(branch, totalSales, date) {
        this.branch = branch;
        this.totalSales = totalSales;
        this.date = date
    }
}

function CalculateBestBranch(sales) {
var branchSales = { key: "", value: 0 };



// TODO: order branchSales by value, highest first
// TODO: return the key of the highest value
const highestValue = 0;
return highestValue;

}

class SalesItem {
  constructor(branch, totalSales, date) {
    this.branch = branch;
    this.totalSales = totalSales;
    this.date = date;
  }
}

const sales = [
  new SalesItem('branch-1', 60, '2022-01-26'),
  new SalesItem('branch-2', 90, '2022-01-26'),
  new SalesItem('branch-1', 20, '2022-01-27'),
  new SalesItem('branch-2', 30, '2022-01-27'),
];

function calculateBestBranch(sales) {
  const branchSales = [];

  sales.forEach((sale) => {
    if (!branchSales.find((e) => e.key === sale.branch)) {
      branchSales.push({ key: sale.branch, value: sale.totalSales });
    } else {
      let i = branchSales.findIndex((e) => e.key == sale.branch);
      branchSales[i].value += sale.totalSales;
    }
  });

  const sortedBranchSales = branchSales.sort((a, b) => b.value - a.value);

  return sortedBranchSales[0];
}

calculateBestBranch(sales); // { key: 'branch-2', value: 120 }