javascript - 使用 map、filter 和 reduce 变换随时间变化的对象数组
问题描述
我有一个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
Object 中的 Object 计算的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
Object 计算的,但它随时间而变化。我的意思是,我LineChart
的包含 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
可以处理我的问题,但我不知道如何实现。请给我一些解决方案或想法。谢谢!
解决方案
推荐阅读
- javascript - 在插入之前检查 MongoDB 值是否存在
- c# - 选择项目后如何删除组合框
- c++ - 使用 CMake 查找本地目录库
- c++ - 我正在尝试以 1 + 2 的形式输入一个简单的数学表达式
- javascript - ES6,在访问“this”并在派生类中定义常量之前必须调用 super
- javascript - 通过反应在孩子中调用函数的孙子
- java - 在适用于 Linux 的 Windows 子系统 (WSL) 上安装 mitmproxy
- javascript - while 循环条件评估未按预期工作
- java - 扭曲排序
- python - 在python的while循环中更新“To:”电子邮件标题