我有一个reportsList
数组,例如包含1000个report
对象,如下所示:
[
{
"id": 69,
"userId": 8,
"emotion": {
"id": "worried",
"name": "Worried Face",
"colons": ":worried:",
"emoticons": [],
"native": "",
"skin": null,
"unified": "1f61f"
},
"title": "Ut voluptatem in.",
"date": "2018-08-01"
},
{
"id": 119,
"userId": 2,
"emotion": {
"id": "heart_eyes",
"name": "Smiling Face with Heart-Shaped Eyes",
"colons": ":heart_eyes:",
"emoticons": [],
"skin": null,
"native": ""
},
"title": "Maiores inventore sapiente optio est et.",
"date": "2018-08-01"
},
{
"id": 187,
"userId": 18,
"emotion": {
"id": "laughing",
"name": "Smiling Face with Open Mouth and Tightly-Closed Eyes",
"colons": ":laughing:",
"emoticons": [
":>",
":->"
],
"native": "",
"skin": null,
"unified": "1f606"
},
"title": "Voluptatibus qui fugit omnis cumque sapiente.",
"date": "2018-08-01" },
{
"id": 256,
"userId": 20,
"emotion": {
"id": "white_frowning_face",
"name": "White Frowning Face",
"colons": ":white_frowning_face:",
"emoticons": [],
"skin": null,
"native": "☹️",
"unified": "2639-fe0f"
},
"title": "Dolor dolores et ut omnis aperiam.",
"date": "2018-08-01"
},
{
"id": 519,
"userId": 1,
"emotion": {
"id": "heart_eyes",
"name": "Smiling Face with Heart-Shaped Eyes",
"colons": ":heart_eyes:",
"emoticons": [],
"skin": null,
"native": ""
},
"title": "Quis minima odit.",
"date": "2018-08-01"
},
{
"id": 523,
"userId": 9,
"emotion": {
"id": "laughing",
"name": "Smiling Face with Open Mouth and Tightly-Closed Eyes",
"colons": ":laughing:",
"emoticons": [
":>",
":->"
],
"native": "",
"skin": null,
"unified": "1f606"
},
"title": "Hic sed labore exercitationem inventore iusto odio.",
"date": "2018-08-01"
}
]
现在,我正尝试基于reportsList
来转换数据,以使用Chartjs
来将其显示并显示到我的图表中(我的应用程序具有PieChart
和LineChart
)。
但是我有一些我无法解决的问题。
PieChart
是基于emotion
的每个report
对象中的reportsList
对象来计算的。对于emotion
,我将其分为三种类型:positive
,negative
和other
。我的三种类型的状况如下:
reportsList.map(report => {
const emoji = report.emotion.id;
if ((emoji === 'smiley')
|| (emoji === 'heart_eyes')
|| (emoji === 'stuck_out_tongue_winking_eye')
|| (emoji === 'laughing')) {
// positive type
} else if ((emoji === 'white_frowning_face')
|| (emoji === 'disappointed')
|| (emoji === 'worried')) {
// negative type
} else {
// this is other type
}
});
此PieChart
中的数据要求是emotion
的数量,例如:
emotions = [100, 200, 300]
相当于三种情绪类型的数量:positive
,negative
和other
。那就是我期望的输出
LineChart
是基于emotion
对象计算的,但是它随时间而变化。我的意思是,我的LineChart
包含3行,相当于3种情绪值:positive
,negative
和other
。并且数据从2018-06-31
变为2018-09-01
。
这一数据要求是我上面给出的日期范围内情感对象的数量,我的图表包含3条线,因此我需要三个数组来完整填充我的图表:
positiveEmotions = [100, 200, 300, 400]
negativeEmotions = [50, 60, 70, 80]
otherEmotions = [40, 79, 23, 252]
,所有三个数组等效于四个月:6, 7, 8, 9
。
我认为filter
,map
和reduce
可以解决我的问题,但是我不知道如何实现。请给我一些解决方案或想法。谢谢!