WDI-SEA / project-4-issues

Open an issue to receive help on project 4 issues
0 stars 0 forks source link

Can you assign a variable as a key #58

Closed jyang1003 closed 2 years ago

jyang1003 commented 2 years ago

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)

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

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?

TaylorDarneille commented 2 years ago

yeah remember this thing?

setState([e.target.name]:e.target.value)?

TaylorDarneille commented 2 years ago

i.e. just put the variable in square brackets and it's value will be the key

jyang1003 commented 2 years ago

Hi, sorry forgot to close. Figured out bracket notation, thanks!