循环假设我将数组存储在对象Map中,并向我展示户外最有利可图的部门。
我想运行循环遍历数组,我只是在地图中再次访问该数据并且获得部门最高利润(销售额)的最高利润值。
E.g将所有户外销售,硬件和木工结合起来给你提供总和和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'
}];
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);
&#13;
答案 0 :(得分:1)
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'},
];
// Build a obj map and sum up the sales of each department
var sales = salesData.reduce((obj, a) => {
obj[a.department] = (obj[a.department] || 0) + a.sales
return obj
}, {})
// Figure out which department is the highest
var high = Object.keys(sales).reduce((a, b) => sales[a] > sales[b] ? a : b )
console.log(sales)
console.log(high, sales[high])
&#13;
答案 1 :(得分:1)
您可以使用以下.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);
&#13;