javascript - Having trouble displaying the outdoor variable -


the loop suppose give me stored array in object map , show me outdoor profitable department.

i want run loop iterating through array, having trouble accessing data again in map , getting profitable value highest(sales) department.

e.g combine outdoors sales, hardware , carpentry give u total , console.log the "outdoor" highest.

    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) {        (var = 0; < saleslist.length; i++) {                var currentdep = saleslist[i];                if (departmentmap[currentdep] === undefined) {            departmentmap[currentdep] = 0;          }          departmentmap[currentdep] = departmentmap[currentdep] + 1        }        console.log(departmentmap);      }                  mostprofitabledepartment(salesdata);            (var dephigh in departmentmap) {        if (departmentmap[dephigh] > high) {          high = departmentmap[dephigh]        }      }            console.log(high);

you can use combination of .reduce calls below.

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


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -