Closed jyang1003 closed 2 years ago
MERN
I'm trying to get my buttons to work by assigning a variable to a string to use as a key for an object
const thisWeek = () => { props.profile.nutrition.forEach(object => { //check which variable to use if(whichDisplay === 'calories'){ whichNutrient = 'calories' } else if (whichDisplay === 'carbs'){ whichNutrient = 'carbs' } else if (whichDisplay === 'protein') { whichNutrient = 'protein' } else if (whichDisplay === 'fats'){ whichNutrient = 'fats' } console.log('this is object', object) //checks if its same week if (moment(object.date).isSame(moment(props.date), 'week')) { //check day of week and push results into respective arrays // console.log('this is same week') console.log('this is which nutrient after button click', whichNutrient) if (moment(object.date).format('dddd') == 'Sunday') { totalForSunday.push(object.whichNutrient) totalForSunday = [totalForSunday.reduce((a, b) => a + b, 0)] // console.log('sun', totalForSunday) } else if (moment(object.date).format('dddd') == 'Monday') { totalForMonday.push(object.whichNutrient) totalForMonday = [totalForMonday.reduce((a, b) => a + b, 0)] // console.log('Mon', totalForMonday) } else if (moment(object.date).format('dddd') == 'Tuesday') { totalForTuesday.push(object.whichNutrient) totalForTuesday = [totalForTuesday.reduce((a, b) => a + b, 0)] // console.log('Tue', totalForTuesday) } else if (moment(object.date).format('dddd') == 'Wednesday') { totalForWednesday.push(object.whichNutrient) totalForWednesday = [totalForWednesday.reduce((a, b) => a + b, 0)] // console.log('Wed', totalForWednesday) } else if (moment(object.date).format('dddd') == 'Thursday') { totalForThursday.push(object.whichNutrient) totalForThursday = [totalForThursday.reduce((a, b) => a + b, 0)] // console.log('Thur', totalForThursday) } else if (moment(object.date).format('dddd') == 'Friday') { totalForFriday.push(object.whichNutrient) totalForFriday = [totalForFriday.reduce((a, b) => a + b, 0)] // console.log('Fri', totalForFriday) } else if (moment(object.date).format('dddd') == 'Saturday') { totalForSaturday.push(object.whichNutrient) totalForSaturday = [totalForSaturday.reduce((a, b) => a + b, 0)] // console.log('Sat', totalForSaturday) } } })
no error message
the key isn't the right key to access the value
yeah remember this thing?
setState([e.target.name]:e.target.value)?
setState([e.target.name]:e.target.value)
i.e. just put the variable in square brackets and it's value will be the key
Hi, sorry forgot to close. Figured out bracket notation, thanks!
What stack are you using?
(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)
MERN
What's the problem you're trying to solve?
I'm trying to get my buttons to work by assigning a variable to a string to use as a key for an object
Post any code you think might be relevant (one fenced block per file)
If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?
no error message
What is your best guess as to the source of the problem?
the key isn't the right key to access the value
What things have you already tried to solve the problem?