更改JS中特定值的对象标签
我正在研究react.js中的图表,我想显示按月排序的数据。在Django中,我创建了用于显示JSON的视图,每月都有总数,看起来这样:
[
{
"month": "2022-06-01T00:00:00+02:00",
"total": 4
},
{
"month": "2022-08-01T00:00:00+02:00",
"total": 1
}
]
]
想对该数组中的每个对象进行排序,然后将“月度”从数字到月份更改为“月”,因此:
[
{
"month": "june",
"total": 4
},
{
"month": "august",
"total": 1
}
]
]
我 我正在使用Chartist的图表。
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:
[
{
"month": "2022-06-01T00:00:00+02:00",
"total": 4
},
{
"month": "2022-08-01T00:00:00+02:00",
"total": 1
}
]
]
I would like to sort every object in that array and change value 'month' from numbers to month name, so it would be:
[
{
"month": "june",
"total": 4
},
{
"month": "august",
"total": 1
}
]
]
For generating charts i'm using chartist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将全日期存储为单独的密钥(称为日期),并使用预定义的数组为每个月的名称设置单独的函数。这是您要使用的代码:
作为替代方案,您还可以为阵列中的每个对象制作一个方法,但是每次要获得一个月时,您都需要运行此方法。这是为此的代码:
您会这样得到:
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:
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:
And you would get it like this: