Getting "undefined" trying to create a function

getting synonym thesaurus
getting ready
obtaining
i'm getting meaning
is getting a word
trying to get synonym
getting over it apk
getting in a sentence

So what I want to do, is filter through an array use forEach() and for each session check the dayOfWeek it is on (Monday, Tuesday, ... ). If it is a certain day, it will multiple the price and the duration of the session to come up with an income for that day and add it to the 'mondayIncome', 'tuesdayIncome', ... and so on. I want to later use this information to create a chart. When I console.log() the function, it returns undefined, why is that? Also, would this process be easier if I had a state?

I have other files such as AddSession.js and Chart.js but I decided to make the function in my Dashboard where the chart resides, is that okay?

Here is my code:

import React, { Component } from 'react'
import Income from './Income'
import Chart from './Chart'
import {connect} from 'react-redux'


class Dashboard extends Component {

  dayOfWeek = () => {
    const {sessionList} = this.props;
    let sundayIncome = 0;
    let mondayIncome = 0;
    let tuesdayIncome = 0;
    let wednesdayIncome = 0;
    let thursdayIncome = 0;
    let fridayIncome = 0;
    let saturdayIncome = 0;

    sessionList.forEach(session => {
        if (session.dayOfWeek === 'Sunday') {
            sundayIncome += (session.price * session.duration) 
            return sundayIncome
        }
        else if (session.dayOfWeek === 'Monday') {
            mondayIncome += (session.price * session.duration) 
            return mondayIncome
        }
        else if (session.dayOfWeek === 'Tuesday') {
            tuesdayIncome += (session.price * session.duration) 
            return tuesdayIncome
        }
        else if (session.dayOfWeek === 'Wednesday') {
            wednesdayIncome += (session.price * session.duration) 
            return wednesdayIncome
        }
        else if (session.dayOfWeek === 'Thursday') {
            thursdayIncome += (session.price * session.duration) 
            return thursdayIncome
        }
        else if (session.dayOfWeek === 'Friday') {
            fridayIncome += (session.price * session.duration) 
            return fridayIncome
        }
        else {
            saturdayIncome += (session.price * session.duration) 
            return saturdayIncome
        }
    })
}
  render() {
    console.log(this.dayOfWeek())
    return (
      <div className="container mt-5">

        <Chart/>
        <Income/>
        <hr/>

      </div>
    )
  }
}

const mapStateToProps = (state) => {
  return {
      sessionList : state.sessions.sessionList
  }
}

export default connect(mapStateToProps)(Dashboard)

I am not sure how your data structure is, but hopefully the below will give an idea of how to do this. I have also mocked this function in a codeSandBox for you so you can see it displaying the correct data. I have not rounded the numbers (Will leave you to format it how you like).

The link to the codeSandBox is here: https://codesandbox.io/s/8xon4yxo58

class Dashboard extends Component {
  dayOfWeek = () => {
    const { sessionList } = this.props;

    let sundayIncome = 0;
    let mondayIncome = 0;
    let tuesdayIncome = 0;
    let wednesdayIncome = 0;
    let thursdayIncome = 0;
    let fridayIncome = 0;
    let saturdayIncome = 0;

    sessionList.forEach(session => {
      if (session.dayOfWeek === "Sunday") {
        sundayIncome += session.price * session.duration;
      } else if (session.dayOfWeek === "Monday") {
        mondayIncome += session.price * session.duration;
      } else if (session.dayOfWeek === "Tuesday") {
        tuesdayIncome += session.price * session.duration;
      } else if (session.dayOfWeek === "Wednesday") {
        wednesdayIncome += session.price * session.duration;
      } else if (session.dayOfWeek === "Thursday") {
        thursdayIncome += session.price * session.duration;
      } else if (session.dayOfWeek === "Friday") {
        fridayIncome += session.price * session.duration;
      } else {
        saturdayIncome += session.price * session.duration;
      }
    });

    return [
      mondayIncome,
      tuesdayIncome,
      wednesdayIncome,
      thursdayIncome,
      fridayIncome,
      saturdayIncome,
      sundayIncome
    ];
  };

  render() {
    const sessionData = this.dayOfWeek();

    return (
      <div className="container mt-5">
        <p>
          {`Monday: ${sessionData[0]}`} <br />
          {`Tuesday: ${sessionData[1]}`} <br />
          {`Wednesday: ${sessionData[2]}`} <br />
          {`Thursday: ${sessionData[3]}`} <br />
          {`Friday: ${sessionData[4]}`} <br />
          {`Saturday: ${sessionData[5]}`} <br />
          {`Sunday: ${sessionData[6]}`} <br />
        </p>
      </div>
    );
  }
}

const sessionList = [
  {
    dayOfWeek: "Sunday",
    price: 4.99,
    duration: 40
  },
  {
    dayOfWeek: "Tuesday",
    price: 4.99,
    duration: 2
  },
  {
    dayOfWeek: "Friday",
    price: 4.99,
    duration: 1
  },
  {
    dayOfWeek: "Sunday",
    price: 4.99,
    duration: 6
  },
  {
    dayOfWeek: "Monday",
    price: 4.99,
    duration: 7
  },
  {
    dayOfWeek: "Wednesday",
    price: 4.99,
    duration: 40
  },
  {
    dayOfWeek: "Sunday",
    price: 4.99,
    duration: 3
  },
  {
    dayOfWeek: "Sunday",
    price: 4.99,
    duration: 9
  },
  {
    dayOfWeek: "Thursday",
    price: 4.99,
    duration: 12
  }
];

The above produces this output

Monday: 34.93 
Tuesday: 9.98 
Wednesday: 199.60000000000002 
Thursday: 59.88 
Friday: 4.99 
Saturday: 0 
Sunday: 289.42

Hope I have understood your question correctly and the above is helpful.

getting, come, go, or make progress eventually or with some difficulty. There are two options for getting an appointment: For an automated appointment system available 24 hours a day, call 1-877-487-2778; TDD/TTY: 1-888-874-7793. OR use the online passport appointment system. At your appointment, you’ll need to provide proof of immediate travel, an expedited fee, and the items listed below to renew your passport.

forEach don't return anything ( it's return value is undefined ), you need to store value in variable and return at end of function.

    dayOfWeek = () => {
    const {sessionList} = this.props;
    let sundayIncome = 0;
    let mondayIncome = 0;
    let tuesdayIncome = 0;
    let wednesdayIncome = 0;
    let thursdayIncome = 0;
    let fridayIncome = 0;
    let saturdayIncome = 0;

    sessionList.forEach(session => {
        if (session.dayOfWeek === 'Sunday') {
            sundayIncome += (session.price * session.duration) 
        }
        else if (session.dayOfWeek === 'Monday') {
            mondayIncome += (session.price * session.duration) 
        }
        else if (session.dayOfWeek === 'Tuesday') {
            tuesdayIncome += (session.price * session.duration) 
        }
        else if (session.dayOfWeek === 'Wednesday') {
            wednesdayIncome += (session.price * session.duration) 
        }
        else if (session.dayOfWeek === 'Thursday') {
            thursdayIncome += (session.price * session.duration) 
        }
        else if (session.dayOfWeek === 'Friday') {
            fridayIncome += (session.price * session.duration) 
        }
        else {
            saturdayIncome += (session.price * session.duration) 
        }
    })
 return {sundayIncome ,mondayIncome,tuesdayIncome,wednesdayIncome,thursdayIncome,fridayIncome,saturdayIncome}
}

GETTING, Synonyms for getting at Thesaurus.com with free online thesaurus, antonyms, and definitions. Find descriptive alternatives for getting. Getting Started with AWS Learn the fundamentals and start building on AWS now. Get to Know the AWS Cloud. AWS fundamentals you need to know before launching your

You should return the value and use reduce instead of forEach.

It would look like:

...
return sessionList.reduce(
  (income, session) => {
    income[session.dayOfWeek] += (session.price * session.duration);
    return income;
  },
  { Monday: 0, Thueday: 0, Wednesday: 0, Thursday: 0, Friday:0, Saturday: 0, Sunday: 0 }
);
...

Cf reduce for details

As of where to create this (use a state or not) it a more architectural problem that depend on where and how you intend to use this information.

Getting Synonyms, Getting Antonyms, Here they remained three or four days, getting or taking over 40 steers per diem. From the Cambridge English Corpus. A crucial precondition for� Getting Pregnant If you're wondering how to get pregnant, we'll tell you everything you need to know, from how long it usually takes to the right time to have sex and how to know when you're ovulating.

Getting definition and meaning, 261 synonyms of getting from the Merriam-Webster Thesaurus, plus 366 related words, definitions, and antonyms. Find another word for getting. Saving lives is the greatest job in the world. Everything we do aims to secure the safest and most reliable care and cure within healthcare and life science.

GET (verb) definition and synonyms, Getting definition: Getting is the present participle of → get 1 . | Meaning, pronunciation Duration: 0:13 Posted: 2 days ago Appleseed’s delivers classic clothes, shoes and accessories for women. Style for every occasion dressy, casual, sleep and lounge in Misses, Petite, and Women’s Plus sizes available in our women’s shirts, pants, jackets, Foxcroft, sweaters, & loungewear.

Getting dictionary definition, It's getting late – I have to go. If that spot gets any bigger you should go to the doctor. get wet/cold/tired/ill etc: It was raining and we all got wet. get angry/upset etc� David Allen's Getting Things Done® (GTD®) is the work-life management system that alleviates overwhelm, and instills focus, clarity, and confidence.David Allen's Getting Things Done®

Comments
  • you dont' return a value from dayOfWeek function
  • also having a return in sessionList.forEach(session => { makes no sense, since the returned value is lost
  • Got it! Thank you :) I didn't realize forEach doesn't return a value. This helped alot!
  • I haven't downvoted, but variables are already defined outside of forEach.
  • I am not the down voter, but you answer it isn't a solution to the problem as it calculate the total income and not the income per day
  • @AZ_ yes but as OP used forEach you wont be able to know which one to return. as forEach doesn't return values
  • @AZ_ if you don't return anything from function it will implicitly return undefined so returning undefined and not returning anything is same thing
  • But why use a forEach if you want to return a result and not reduce?
  • Your code has some issues mate , income[session.dayOfWeek] += (session.price * session.duration); will result in NaN as you have [] as accumulator, you should not use array when you want index to be anything else than number you should use Map or Object