2016-11-10 56 views
0

該循環假設我將數組存儲在對象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);

回答

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])

+0

不完全是,但我確實有做如上指示。我無法得到正確的。 @Endless – Duvall912

+0

Sry,誤解了你的問題,更新了答案 – Endless

+0

我實際上並不瞭解代碼,請你澄清一下,而不使用reduce函數。 @Endless – Duvall912

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);

+0

代碼應該是像上面提到的那樣構建,只是糾正了它不想去的地方。我不想複製你的代碼,只需要澄清它就可以使用for in循環和對象映射來運行代碼。 @Jamiec – Duvall912

+0

@ Duvall912你的代碼結構嚴重不全(全局變量被函數修改,壞習慣)。花時間瞭解更好的方式來實現您的最終目標。 – Jamiec