JavascriptSkills.com Uncategorized How to use Reduce in an Object

How to use Reduce in an Object

Basically, reduce adds everything in an Array.

let userInputPushups = 40;
let sum =0; 


let Week = {

Sunday:0,

Monday:[{     

numberOfPushups:[],
numberOfSitups:[],


}],

Tuesday:2,
Wednesday:3,
Thursday:4,
Friday:5,
Saturday:[{     

numberOfPushups:[3,3,3],
numberOfSitups:[],


}],


}


Week.Saturday[0].numberOfPushups.push(userInputPushups);

console.log(Week.Saturday[0].numberOfPushups)
console.log(Week.Saturday[0].numberOfSitups)

//output: [3, 3, 3, 40]
//output: []

sum =  Week.Saturday[0].numberOfPushups.reduce((a,b) => a +b,0);

console.log(sum)

https://jsfiddle.net/5jexzt3u/

anyArray.reduce( (a,b) => a + b,0 ); This formula will sum up all arrays values. AnyArray is the location of the array, it can be in an object, or just a regular Array.

let pu = document.querySelector("#pushups");
let sb = document.querySelector("#submit")




sb.addEventListener("click", () => {

let num = +pu.value;
//Changes String into a number with the +

//this if statament returns nothing if user inputs a non digit number
if(!num)
{
return;
}
//call coolObj
let isCoolObj = coolObj();
trackPushups(num,isCoolObj);


});

//create an Obj inside a function
function coolObj()
{


let Week = {

Sunday:0,

Monday:[{     

numberOfPushups:[1],
numberOfSitups:[],


}],

Tuesday:2,



Wednesday:[{     

numberOfPushups:[],
numberOfSitups:[],


}],
}

return Week;

}


//this function tracks the number of pushups the user enters

function trackPushups(numberOfP,whateverObj)
{


switch(getDay())
{

case 0:
console.log("Today is Sunday");
break;
case 1:
console.log("Today is Monday");
break;
case 2:
console.log("Today is Tuesday");
break;
case 3:
console.log("Today is Wednesday");
whateverObj.Wednesday[0].numberOfPushups.push(numberOfP)
console.log(numberOfP)
break;
case 4:
console.log("Today is Thursday");
break;
case 5:
console.log("Today is Friday");
break;
case 6:
console.log("Today is Saturday");
break;

}

}


function getDay()
{

const date = new Date();
let day = date.getDay();
return day;

}




//https://jsfiddle.net/hrop9gn2/2/
  • Line: 25 function coolObj() returns Week Object.
  • Line 18 We store the object into this variable: let isCoolObj = coolObj();
  • Line 19 We pass the object into trackPushups(num,isCoolObj);
  • Line 61: function trackPushups(numberOfP,whateverObj) WhateverOBJ is the same as isCoolObj.
  • Line 79: We store the userInput, and we use PUSH to enter the input into the Object Array whateverObj.Wednesday[0].numberOfPushups.push(numberOfP)
let pu = document.querySelector("#pushups");
let sb = document.querySelector("#submit")
let arr = [];
let isCoolObj = coolObj();



sb.addEventListener("click", () => {

 let num = +pu.value;
//Changes String into a number with the +

//this if statament returns nothing if user inputs a non digit number
if(!num)
{
return;
}
//call coolObj

let sumPushups = trackPushups(num,isCoolObj);
console.log(sumPushups + "sumPushups")
console.log(isCoolObj.Wednesday[0].numberOfPushups)


});

//create an Obj inside a function
function coolObj()
{


let Week = {

Sunday:0,

Monday:[{     

numberOfPushups:[1],
numberOfSitups:[],


}],

Tuesday:2,



Wednesday:[{     

numberOfPushups:[],
numberOfSitups:[],


}],
}

return Week;

}


//this function tracks the number of pushups the user enters

function trackPushups(numberOfP,whateverObj)
{
let sum =0;

switch(getDay())
{

case 0:
console.log("Today is Sunday");
break;
case 1:
console.log("Today is Monday");
break;
case 2:
console.log("Today is Tuesday");
break;
case 3:
console.log("Today is Wednesday");
whateverObj.Wednesday[0].numberOfPushups.push(numberOfP)
sum = whateverObj.Wednesday[0].numberOfPushups.reduce( (a,b) => a + b,0);


break;
case 4:
console.log("Today is Thursday");
break;
case 5:
console.log("Today is Friday");
break;
case 6:
console.log("Today is Saturday");
break;

}

return sum;

}


function getDay()
{

const date = new Date();
let day = date.getDay();
return day;

}

console.log(arr)

//https://jsfiddle.net/hrop9gn2/4/
  • Line 4: We need to create the object before it can be accessible.
  • Line 83: We store the the sum in the sum variable. sum = whateverObj.Wednesday[0].numberOfPushups.reduce( (a,b) => a + b,0);
  • Line 20: We store the return statement of the function trackPushups(num,isCoolObj);. let sumPushups = trackPushups(num,isCoolObj);

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post