【发布时间】:2021-12-05 01:31:36
【问题描述】:
我正在使用 reaviz 是 React 的图表工具。我正在创建一个习惯跟踪器应用程序,并希望在一系列条形图上直观地显示用户输入最后一周的数据。我的数据库中的数据结构如下:
habits = [
{
habit: "Cook Dinner",
inputs: [
{
input: 'Success',
date: 'Oct 5th 21'
},
{
input: 'Fail',
date: 'Oct 6th 21'
},
{
input: 'Skip',
date: 'Oct 7th 21'
}]
},
{
habit: "Clean House",
inputs: [
{
input: 'Fail',
date: 'Oct 5th 21'
},
{
input: 'Fail',
date: 'Oct 6th 21'
},
{
input: 'Fail',
date: 'Oct 7th 21'
}]
},
{
habit: "Cook Dinner",
inputs: [
{
input: 'Success',
date: 'Oct 5th 21'
},
{
input: 'Fail',
date: 'Oct 6th 21'
},
{
input: 'Skip',
date: 'Oct 7th 21'
}]
}]
所以现在我想找到一种方法将我的数据映射到图表所需的数据结构,例如上述数据的输出将是这样的。我需要计算当天有多少输入是成功、失败或跳过:
habitsMappedData = [
{
key: 'Oct 5th 21',
data: [
{
key: 'Success',
data: 2
},
{
key: 'Fail',
data: 1
},
{
key: 'Skip',
data: 0
}
]
}, {
key: 'Oct 6th 21',
data: [
{
key: 'Sucess',
data: 0
},
{
key: 'Fail',
data: 3
},
{
key: 'Skip',
data: 0
}
]
}, {
key: 'Oct 7th 21',
data: [
{
key: 'Success',
data: 0
},
{
key: 'Fail',
data: 1
},
{
key: 'Skip',
data: 2
}
]
},
]
非常感谢您浏览此内容。如果需要任何澄清,请告诉我。
【问题讨论】:
标签: javascript arrays reactjs filter mapping