I have a very simple problem but for some reason I can't find the answer for it.
label.forEach(function(value){
months.push(value['month']);
revenue.push(value['revenue']);
});
The label
is an array of numbers and revenue, in my case this is
[
{month: 9, revenue: 400},
{month: 11, revenue: 500},
{month: 12, revenue: 600}
]
This is a forEach loop in javascript, it pushes revenue and a month number into two seperate arrays, the problem is that the month is a number (e.g. 12) but I want the .push() to push a month name instead (December), I can't seem to find anything so I was hoping anyone here could help me out.
This below function takes the month number (like 1 or 2) as an argument and returns the month name in string format. Let's learn how the above function works: First we initialized a JavaScript new Date() constructor. We invoked a setMonth() method on JavaScript date Object by passing the month-1 as an argument.
Please do as follows: Select a blank cell next to the sales table, type the formula =TEXT(A2*29,"mmm") (Note: A2 is the first number of the Month list you will convert to month name), and then drag the AutoFill Handle down to other cells. Now you will see the numbers (from 1 to 12) are converted to normal month names.
Intl.DateTimeFormat('en', { month: 'long' }).format(new Date('1')); // January
Can be achieved using Internationalization API
var months = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
var selectedMonthName = months[value['month']];
look at the links
stack 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With