score:1
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.
score:1
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}
}
score:1
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.
Source: stackoverflow.com
Related Query
- Getting "undefined" trying to create a function
- I'm trying to use chart.js in angular to create a simple bar chart but I'm getting an error as 'legend' type doesn't exist for options
- code works fine on jsfiddle but one function is not working on website
- create Chart.js after getting data from api
- Using Chart.js onClick function, can you execute code from an undefined or null result?
- Trying to create chartjs constructor
- function returns undefined in AngularJS
- Getting Error regarding :Cannot read property 'length' of undefined
- Trying to export chart with Chartjs and React but getting erorr
- .datasets is undefined when trying to access bars from chart.js
- Trying to create a custom tooltip in a doughnut chart using chartsjs, but it is not working
- Getting TypeError: (intermediate value).Bar is not a function with react and chart.js
- getting additional value fields from data source for dx.chartjs doughnut chart
- Getting "Uncaught TypeError: Cannot read properties of undefined (reading 'map')" when using Chartjs inside react axios method
- Background color does not work when trying to create my data before using scatter chart with chart.js
- Created an onclick function to remove data from a line chart in ChartsJs, but getting "Cannot read property 'data' of undefined" error
- js error: while trying to display graph using chart.js: Uncaught TypeError: Cannot read property 'call' of undefined
- Getting the HTML code of a chart created by chart.js
- How to run Chart.js samples using source code
- Trying to create something from chart.js
- I'm getting the error "TypeError: Cannot read property 'extend' of undefined" when trying to import chartjs-plugin-datalabels
- chart.js Failed to create chart: can't acquire context from the given item
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- Chart.js pie tooltip getting cut
- create a multi line chart using Chart.js
- How to create two x-axes label using chart.js
- Chart Js Cannot read property 'length' of undefined
- How to create rounded bars for Bar Chart.js v2?
- Uncaught TypeError: Cannot read property 'offsetWidth' of undefined - chart.js
- Chartjs cannot read property datasets of undefined
More Query from same tag
- How to generate multi bar chart in Chart.js with php & sql
- using react js to create a website that could read .csv file and show in chartjs
- Is there a way to store a chart as an image directly to server using Chart.js?
- Why the bar are not aligned the y axis
- How to set vertical lines for new day on x-axis in ChartJS v3.x
- How to Create a Custom Logarithmic Axis in Chart.js
- ChartJS number shows up on top of number
- ChartJS hover/tooltips: selecting the correct points in the datasets based on x-value
- Set size chartjs in div
- Remove the vertical line in the chart js line chart
- ChartJS ticks values are not correct
- Chart JS no animation line chart
- Chart js scatter - display label on hover
- math.max() doesn't work in IE
- Unable to hide Legend in Chartjs with PrimeFaces7.0
- ChartsJS Legend not showing in Angular11
- AddData in Chart.js 2.0
- Chart.js v2, remove padding/margin from radar chart
- Leaflet polygon display charts after click
- How to use vanilla js in reactjs to create a chart?
- Chart.js pie chart to display "No Data" if the datasets are empty
- Dynamic Chart Data
- How can I tighten letter spacing on labels with ChartJS?
- Calculate value in tooltip in ChartJS
- Make a chart clickable when placed under another SVG <View>
- Combining charting with the map
- ChartJs + ie11 doesn't work
- Django 1.11 - child template not inheriting from parent
- bind first property value of an array of object into chart.js
- How do I use ChartJS with a background color in the space between two line charts?