我需要通过for in
循环显示最高值。
我有三个部门要显示,但是在for in
循环之后它不想接受并给我最高价值。
到目前为止,代码成功循环并为我提供了所需的结果。它应该console.log
我将户外作为最有利可图的部门。
这是我的作业,只是澄清因为我不是在寻找答案,只是澄清如何去做。简单而彻底。
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'},
];
function mostProfitableDepartment(salesList) {
//group by department`
var salesForEachDepartment = {};
var high = 0;
//loop through all the sales
for (var i = 0; i < salesList.length; i++) {
//salesForEachDepartment += salesList[i];
var deptDailySale = salesList[i];
//check if the current department is in the salesForEachDepartment map
if (salesForEachDepartment[deptDailySale.department] === undefined) {
salesForEachDepartment[deptDailySale.department] = 0;
}
salesForEachDepartment[deptDailySale.department] += deptDailySale.sales;
//deptDailySale.department
//if ()
}
console.log(salesForEachDepartment);
}
mostProfitableDepartment(salesData);
for (var highestDepSale in salesForEachDepartment[deptDailySale.sales]) {
if (salesForEachDepartment[highestDepSale.sales] > highestDepSale) {
highestDepSale = salesForEachDepartment[highestDepSale.sales];
}
console.log(highestDepSale);
}
答案 0 :(得分:0)
您应该在循环
之前先对salesList进行排序salesList = salesList.sort(function (a, b) {
return b.sales - a.sales; // desc
});
答案 1 :(得分:0)
salesForEachDepartment
的结构是
{
"hardware": 32505,
"outdoor": 42525,
"carpentry": 37267
}
因此,当您执行salesForEachDepartment[deptDailySale.sales]
时,您只能访问一个值。因此,您只获得第一个价值。
你只需要使用
for (var k in salesForEachDepartment) {
if (salesForEachDepartment[k] > max)
max = salesForEachDepartment[k];
}
您的代码已更新
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'},
];
function mostProfitableDepartment(salesList) {
//group by department`
var salesForEachDepartment = {};
var high = 0;
//loop through all the sales
for (var i = 0; i < salesList.length; i++) {
//salesForEachDepartment += salesList[i];
var deptDailySale = salesList[i];
//check if the current department is in the salesForEachDepartment map
if (salesForEachDepartment[deptDailySale.department] === undefined) {
salesForEachDepartment[deptDailySale.department] = 0;
}
salesForEachDepartment[deptDailySale.department] += deptDailySale.sales;
//deptDailySale.department
//if ()
}
console.log(salesForEachDepartment);
var max = 0;
for (var k in salesForEachDepartment) {
if (salesForEachDepartment[k] > max)
max = salesForEachDepartment[k];
}
console.log(max)
}
mostProfitableDepartment(salesData);
您可以使用Array.forEach
+ for..in
+ Math.max
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 groupedData = {}
salesData.forEach(function(o){
groupedData[o.department] = (groupedData[o.department] || 0) + o.sales;
});
var max = 0;
for (var k in groupedData){
max = Math.max(groupedData[k], max);
}
console.log(max)