该循环假定是让我将数组存储在对象Map中,并向我展示室外是最赚钱的部门。

我想运行遍历数组的循环,只是在再次访问地图中的数据以及部门获得最高利润值(销售)时遇到麻烦。

例如,将所有户外销售,硬件和木工结合起来,得出总计和控制台。记录最高的“室外”。



    var salesData = [{
      department: 'hardware',
      sales: 4500,
      day: 'Monday'
    }, {
      department: 'outdoor',
      sales: 1500,
      day: 'Monday'
    }, {
      department: 'carpentry',
      sales: 5500,
      day: 'Monday'
    }, {
      department: 'hardware',
      sales: 7500,
      day: 'Tuesday'
    }, {
      department: 'outdoor',
      sales: 2505,
      day: 'Tuesday'
    }, {
      department: 'carpentry',
      sales: 1540,
      day: 'Tuesday'
    }, {
      department: 'hardware',
      sales: 1500,
      day: 'Wednesday'
    }, {
      department: 'outdoor',
      sales: 8507,
      day: 'Wednesday'
    }, {
      department: 'carpentry',
      sales: 8009,
      day: 'Wednesday'
    }, {
      department: 'hardware',
      sales: 12000,
      day: 'Thursday'
    }, {
      department: 'outdoor',
      sales: 18007,
      day: 'Thursday'
    }, {
      department: 'carpentry',
      sales: 6109,
      day: 'Thursday'
    }, {
      department: 'hardware',
      sales: 7005,
      day: 'Friday'
    }, {
      department: 'outdoor',
      sales: 12006,
      day: 'Friday'
    }, {
      department: 'carpentry',
      sales: 16109,
      day: 'Friday'
    }];
    var departmentMap = {};
    var high = "";

    function mostProfitableDepartment(salesList) {
      for (var i = 0; i < salesList.length; i++) {

        var currentDep = salesList[i];

        if (departmentMap[currentDep] === undefined) {
          departmentMap[currentDep] = 0;
        }
        departmentMap[currentDep] = departmentMap[currentDep] + 1
      }
      console.log(departmentMap);
    }


    mostProfitableDepartment(salesData);

    for (var depHigh in departmentMap) {
      if (departmentMap[depHigh] > high) {
        high = departmentMap[depHigh]
      }
    }

    console.log(high);

最佳答案

您可以如下组合使用.reduce调用。



var salesData = [{
      department: 'hardware',
      sales: 4500,
      day: 'Monday'
    }, {
      department: 'outdoor',
      sales: 1500,
      day: 'Monday'
    }, {
      department: 'carpentry',
      sales: 5500,
      day: 'Monday'
    }, {
      department: 'hardware',
      sales: 7500,
      day: 'Tuesday'
    }, {
      department: 'outdoor',
      sales: 2505,
      day: 'Tuesday'
    }, {
      department: 'carpentry',
      sales: 1540,
      day: 'Tuesday'
    }, {
      department: 'hardware',
      sales: 1500,
      day: 'Wednesday'
    }, {
      department: 'outdoor',
      sales: 8507,
      day: 'Wednesday'
    }, {
      department: 'carpentry',
      sales: 8009,
      day: 'Wednesday'
    }, {
      department: 'hardware',
      sales: 12000,
      day: 'Thursday'
    }, {
      department: 'outdoor',
      sales: 18007,
      day: 'Thursday'
    }, {
      department: 'carpentry',
      sales: 6109,
      day: 'Thursday'
    }, {
      department: 'hardware',
      sales: 7005,
      day: 'Friday'
    }, {
      department: 'outdoor',
      sales: 12006,
      day: 'Friday'
    }, {
      department: 'carpentry',
      sales: 16109,
      day: 'Friday'
    }];

var aggregated = salesData.reduce(function(p,c){
   if(p[c.department] == undefined)
     p[c.department] = 0;
   p[c.department]+= c.sales;
   return p;
 },{});

console.log("total sales",aggregated);

var highest = Object.keys(aggregated).reduce(function(p,c){
  if(aggregated[c]>p.value)
    return {key:c,value:aggregated[c]}
  return p;
},{key:'notset',value:0});

console.log("best department", highest);

关于javascript - 无法显示室外变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40527259/

10-17 03:01