Skip to content
Advertisement

Change object label on specific value in js

I’m working on charts in React.js and i want to display data sorted by month. In django i’ve created view for displaying json with total events per month and it looks like this:

JavaScript

I would like to sort every object in that array and change value ‘month’ from numbers to month name, so it would be:

JavaScript

For generating charts i’m using chartist.

Advertisement

Answer

You could store the full date as a seperate key (called date), and set the month in a seperate function using a predefined array for the names of each month. Here is the code you would use:

JavaScript

As an alternative, you could also make a method for each object in the array that gets the month, but you would need to run this everytime you want to get the month. Here is the code for that:

JavaScript

And you would get it like this:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement