使用map,filter和reduce变换对象随时间变化的数组

时间:2018-08-06 08:41:45

标签: javascript

我有一个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来将其显示并显示到我的图表中(我的应用程序具有PieChartLineChart)。

但是我有一些我无法解决的问题。

  1. PieChart是基于emotion的每个report对象中的reportsList对象来计算的。对于emotion,我将其分为三种类型:positivenegativeother

我的三种类型的状况如下:

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]相当于三种情绪类型的数量:positivenegativeother。那就是我期望的输出

  1. LineChart是基于emotion对象计算的,但是它随时间而变化。我的意思是,我的LineChart包含3行,相当于3种情绪值:positivenegativeother。并且数据从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

我认为filtermapreduce可以解决我的问题,但是我不知道如何实现。请给我一些解决方案或想法。谢谢!

0 个答案:

没有答案