遇到麻烦,它没有返回价值的权利

时间:2016-11-11 12:13:51

标签: javascript

for in应该将Friday显示为本周最赚钱的一天,但它会在星期五之外的对象地图中返回一周中所有其他日期。

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 dayForEachDepartment = {};

 function mostProfitableDay(weekdays){
 (var i = 0; i < weekdays.length; i++) {

var profitableDay = weekdays[i]

     if ( dayForEachDepartment[profitableDay.day] === undefined){
      dayForEachDepartment[profitableDay.day] = 0;
    }

dayForEachDepartment[profitableDay.day] += (profitableDay.sales);
}
}
 mostProfitableDay(salesData);
//ObjectMap holding the day's most sold on that specific day.
 var daySoldMost ={
   day : '',
  sales: 0
   };

for (var daysWithSales in dayForEachDepartment){
 if (dayForEachDepartment[daysWithSales] > daySoldMost.sales) {
  daySoldMost.day = daysWithSales;
  daySoldMost.sales = dayForEachDepartment[daysWithSales]
  }
 console.log(daySoldMost);
 }

2 个答案:

答案 0 :(得分:1)

如果将console.log(daySoldMost);移到for循环之外,则会得到想要的结果。顺便说一句,星期四是销售额最高的那个。

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 dayForEachDepartment = {};

function mostProfitableDay(weekdays) {
    for (var i = 0; i < weekdays.length; i++) {
        var profitableDay = weekdays[i]
        if (dayForEachDepartment[profitableDay.day] === undefined) {
            dayForEachDepartment[profitableDay.day] = 0;
        }
        dayForEachDepartment[profitableDay.day] += (profitableDay.sales);
    }
}
mostProfitableDay(salesData);
//ObjectMap holding the day's most sold on that specific day.
var daySoldMost = {
    day: '',
    sales: 0
};

for (var daysWithSales in dayForEachDepartment) {
    if (dayForEachDepartment[daysWithSales] > daySoldMost.sales) {
        daySoldMost.day = daysWithSales;
        daySoldMost.sales = dayForEachDepartment[daysWithSales]
    }
}
console.log(dayForEachDepartment);
console.log(daySoldMost);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Bonus,一个带有单循环的更简洁的解决方案。

function mostProfitableDay(weekdays) {
    weekdays.forEach(function (day, i) {
        dayForEachDepartment[day.day] = (dayForEachDepartment[day.day] || 0) + day.sales;
        if (!i || dayForEachDepartment[day.day] > dayForEachDepartment[daySoldMost]) {
            daySoldMost = day.day;
        }
    });
}

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' } ],
    dayForEachDepartment = {},
    daySoldMost,
    daySoldMostObject;

mostProfitableDay(salesData);

daySoldMostObject = {
    day: daySoldMost,
    sales: dayForEachDepartment[daySoldMost]
};

console.log(dayForEachDepartment);
console.log(daySoldMost);
console.log(daySoldMostObject);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

好像我有点太晚了,但我正在解决你的问题。

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:12e3,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 days={Mondays:0,Tuesdays:0,Wednesdays:0,Thursdays:0,Fridays:0};

    salesData.map(v => v['day'] == 'Monday' ? days['Mondays'] += v['sales'] : v['day'] == 'Tuesday' ? days['Tuesdays'] += v['sales'] :
        v['day'] == 'Wednesday' ? days['Wednesdays'] += v['sales'] : v['day'] == 'Thursday' ? days['Thursdays'] += v['sales'] : days['Fridays'] += v['sales']);

    var sortedArray = [];
    for (var key in days) {
        sortedArray.push([key, days[key]])
    }
    var result = sortedArray.sort((a,b) => b[1] - a[1]);

    console.log(`Max profitable day: ${result[0][0]} with income ${result[0][1]}`);
.as-console-wrapper { max-height: 100% !important; top: 0; }