javascript - Having trouble why the split function does not work -


i trying split salesdata , want split() comma after each object.

this homework.i trying store in object map , calculation there. cant past split function failing me.

i have provided code below show end result profitable department is.

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 = {};  function mostprofitabledepartment() {  var departmentsplit = salesdata.split(',');    (var = 0; < departmentsplit.length; i++) {    var currentdep = departmentsplit[i]   if (departmentmap[currentdep] === undefined) {     departmentmap[currentdep] = 0;   }   departmentmap[currentdep] = departmentmap[currentdep]++ } console.log(departmentmap); }mostprofitabledepartment(salesdata); 

you might follows utilizing array.prototype.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'},  ],          deptmap = salesdata.reduce((p,c) => p[c.department] ? (p[c.department].push({sales: c.sales,        	                                                                             day: c.day}),p)        	                                                  : (p[c.department] = [{sales: c.sales,        	                                                                           day: c.day}],p) ,{});  console.log(deptmap);


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 -