如何通过使用日期作为键obj来修改该对象的结构,并将其与所有在同一日期完成的活动配对,

时间:2019-01-23 17:31:35

标签: javascript json typescript object

我有这种物体

  {
    "activities": [
      {
        "05/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            2,
            1
          ],
          "description": "Walk 100km",
          "color": "#E97825"
        }
      },
      {
        "07/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            2,
            1
          ],
          "description": "Walk 100km",
          "color": "#E97825"
        }
      },
      {
        "08/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            2,
            1
          ],
          "description": "Walk 100km",
          "color": "#E97825"
        }
      },
      {
        "12/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            2,
            1
          ],
          "description": "Walk 100km",
          "color": "#E97825"
        }
      },
      {
        "14/01/2019": {
          "completed": false,
          "frequency": [
            7,
            6,
            2,
            1
          ],
          "description": "Walk 100km",
          "color": "#E97825"
        }
      },
      {
        "15/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            2,
            1
          ],
          "description": "Walk 100km",
          "color": "#E97825"
        }
      },
      {
        "19/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            2,
            1
          ],
          "description": "Walk 100km",
          "color": "#E97825"
        }
      },
      {
        "21/01/2019": {
          "completed": false,
          "frequency": [
            7,
            6,
            2,
            1
          ],
          "description": "Walk 100km",
          "color": "#E97825"
        }
      }
    ]
  },
  {
    "activities": [
      {
        "02/01/2019": {
          "completed": true,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      },
      {
        "03/01/2019": {
          "completed": true,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      },
      {
        "04/01/2019": {
          "completed": false,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      },
      {
        "07/01/2019": {
          "completed": false,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      },
      {
        "09/01/2019": {
          "completed": true,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      },
      {
        "10/01/2019": {
          "completed": false,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      },
      {
        "11/01/2019": {
          "completed": false,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      },
      {
        "14/01/2019": {
          "completed": false,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      },
      {
        "16/01/2019": {
          "completed": true,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      },
      {
        "17/01/2019": {
          "completed": false,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      },
      {
        "18/01/2019": {
          "completed": true,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      },
      {
        "21/01/2019": {
          "completed": true,
          "frequency": [
            5,
            4,
            3,
            1
          ],
          "description": "Drink 4lt Water",
          "color": "#ED1E24"
        }
      }
    ]
  },
  {
    "activities": [
      {
        "02/01/2019": {
          "completed": false,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "03/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "04/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "05/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "07/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "08/01/2019": {
          "completed": false,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "09/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "10/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "11/01/2019": {
          "completed": false,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "12/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "14/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "15/01/2019": {
          "completed": false,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "16/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "17/01/2019": {
          "completed": false,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "18/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "19/01/2019": {
          "completed": false,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      },
      {
        "21/01/2019": {
          "completed": false,
          "frequency": [
            7,
            6,
            5,
            4,
            3,
            2,
            1
          ],
          "description": "Study 2",
          "color": "#F4ED59"
        }
      }
    ]
  },
  {
    "activities": [
      {
        "03/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4
          ],
          "description": "Home drinking food2",
          "color": "#00A651"
        }
      },
      {
        "04/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4
          ],
          "description": "Home drinking food2",
          "color": "#00A651"
        }
      },
      {
        "05/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4
          ],
          "description": "Home drinking food2",
          "color": "#00A651"
        }
      },
      {
        "10/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4
          ],
          "description": "Home drinking food2",
          "color": "#00A651"
        }
      },
      {
        "11/01/2019": {
          "completed": true,
          "frequency": [
            7,
            6,
            5,
            4
          ],
          "description": "Home drinking food2",
          "color": "#00A651"
        }
      ...etc etc

我想要实现的是一个看起来像这样的对象

    "05/01/2019": [
      {
        "completed": true,
        "frequency": [ 7,6,2,1],
        "description": "Walk 100km",
        "color": "#E97825"
      },
      {
        "completed": false,
        "frequency": [ 7,2,1],
        "description": "Find yourself",
        "color": "#E97983"
      },
      {
        "completed": true,
        "frequency": [ 7,6,2],
        "description": "Do stuff",
        "color": "#ED1E24"
      }
    ]
  }

我需要将具有相同日期的所有对象归为一个键,该键将是日期本身。任何想法如何解决这个问题,基本上我卡住了,因为我不太擅长js并且我真的无法弄清楚。

编辑

我需要澄清一个事实,拳头对象已经是我创建所需输出的尝试。 来自db的原始对象具有这种结构,(很长很抱歉。)

  {
    "Created_at": 1547491716787,
    "Frequency": {
      "wrapperName": "Set",
      "values": [
        7,
        6,
        2,
        1
      ],
      "type": "Number"
    },
    "IsActive": true,
    "Description": "Walk 100km",
    "Color": "#E97825",
    "Task_id": "00397030-182d-11e9-957b-79c872c75fe1",
    "Username": "AndreaLovati",
    "completed": [
      {
        "Completed_task_id": "31d9bf80-1a5f-11e9-8c36-a5a8bf4d0952",
        "Task_id": "00397030-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "05/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "31bff5f0-1a5f-11e9-bd59-ed2d765dde0f",
        "Task_id": "00397030-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "07/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "32e67490-1a5f-11e9-aefe-517fcd3f43a2",
        "Task_id": "00397030-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "08/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "336393d0-1a5f-11e9-a8eb-fdd6c2427f95",
        "Task_id": "00397030-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "12/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "33689ce0-1a5f-11e9-8a8d-0df9a14e8e3b",
        "Task_id": "00397030-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "14/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "338d14d0-1a5f-11e9-accb-81b1bf6e645f",
        "Task_id": "00397030-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "15/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "33a8da30-1a5f-11e9-9ee5-71b9c8182046",
        "Task_id": "00397030-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "19/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "339726f0-1a5f-11e9-a0ab-fd3515f202f3",
        "Task_id": "00397030-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "21/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      }
    ]
  },
  {
    "Created_at": 1547491778508,
    "Frequency": {
      "wrapperName": "Set",
      "values": [
        5,
        4,
        3,
        1
      ],
      "type": "Number"
    },
    "IsActive": true,
    "Description": "Drink 4lt Water",
    "Color": "#ED1E24",
    "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
    "Username": "AndreaLovati",
    "completed": [
      {
        "Completed_task_id": "31d6d950-1a5f-11e9-aeb1-6125a3683c89",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "02/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "31d97160-1a5f-11e9-b63e-8b3f3f6ae4fb",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "03/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "31dcf3d0-1a5f-11e9-b592-77559d6dddd3",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "04/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "31be2130-1a5f-11e9-8f9d-6d0e82f25331",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "07/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "3382dba0-1a5f-11e9-a03e-8f601bb8fe01",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "09/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "33511d40-1a5f-11e9-a1d8-25703f31cf16",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "10/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "33235680-1a5f-11e9-a822-7dea3c8b1cdc",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "11/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "331bb560-1a5f-11e9-8452-df3432067ab0",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "14/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "32ee63d0-1a5f-11e9-801f-1de09ba0f6cc",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "16/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "33645720-1a5f-11e9-8ada-e3512555d67b",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "17/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "33abc060-1a5f-11e9-9671-a365f84c4552",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "18/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "3206c2f0-1a5f-11e9-a2ce-f322ac0dcc79",
        "Task_id": "250350c0-182d-11e9-957b-79c872c75fe1",
        "Completed_at": "21/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      }
    ]
  },
  {
    "Created_at": 1547492228254,
    "Frequency": {
      "wrapperName": "Set",
      "values": [
        7,
        6,
        5,
        4,
        3,
        2,
        1
      ],
      "type": "Number"
    },
    "IsActive": true,
    "Description": "Study 2",
    "Color": "#F4ED59",
    "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
    "Username": "AndreaLovati",
    "completed": [
      {
        "Completed_task_id": "31da34b0-1a5f-11e9-b1ba-9b60ce34ea57",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "02/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "31d0e5e0-1a5f-11e9-be1e-03fd7ac77105",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "03/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "31c06b20-1a5f-11e9-9df4-6908b490dc95",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "04/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "31bd36d0-1a5f-11e9-8156-339f41cde74d",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "05/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "32025620-1a5f-11e9-949e-edeb36aca5cd",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "07/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "3340c990-1a5f-11e9-8923-0f0234fe0ad2",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "08/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "32c298e0-1a5f-11e9-92dc-d7369261d974",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "09/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "3320e580-1a5f-11e9-88e3-b736a56ec097",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "10/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "3371ebb0-1a5f-11e9-b8f4-3b7fe5f97947",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "11/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "331e2660-1a5f-11e9-8951-2f2da8116cd5",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "12/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "32ed2b50-1a5f-11e9-9f04-0d04d8ede36f",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "14/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "331bb560-1a5f-11e9-ba08-bd99c24f634b",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "15/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "337d5d60-1a5f-11e9-afb3-cfc41a129f15",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "16/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "336d7ee0-1a5f-11e9-a6e3-e7565b2c3c7c",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "17/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "338face0-1a5f-11e9-a008-217893cec5f7",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "18/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "33c080e0-1a5f-11e9-bcca-811b68c02182",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "19/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "31f89220-1a5f-11e9-a698-011b3a104bfc",
        "Task_id": "31151be0-182e-11e9-957b-79c872c75fe1",
        "Completed_at": "21/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      }
    ]
  },
  {
    "Created_at": 1547491542176,
    "Frequency": {
      "wrapperName": "Set",
      "values": [
        7,
        6,
        5,
        4
      ],
      "type": "Number"
    },
    "IsActive": true,
    "Description": "Home drinking food2",
    "Color": "#00A651",
    "Task_id": "9825ea00-182c-11e9-957b-79c872c75fe1",
    "Username": "AndreaLovati",
    "completed": [
      {
        "Completed_task_id": "31d6d950-1a5f-11e9-bc07-6b5899edd081",
        "Task_id": "9825ea00-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "03/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "31c1a3a0-1a5f-11e9-ac63-67f77c25fdf1",
        "Task_id": "9825ea00-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "04/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "31cec300-1a5f-11e9-9451-ab6c747e6335",
        "Task_id": "9825ea00-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "05/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "330d3670-1a5f-11e9-9f17-c91078a7bd2b",
        "Task_id": "9825ea00-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "10/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "333f42f0-1a5f-11e9-8932-d73bbd49f102",
        "Task_id": "9825ea00-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "11/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "32b9e650-1a5f-11e9-926b-a152649220b1",
        "Task_id": "9825ea00-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "12/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "332133a0-1a5f-11e9-b732-678a06dfe98a",
        "Task_id": "9825ea00-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "17/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "33b84380-1a5f-11e9-a0c4-ff4882b711ba",
        "Task_id": "9825ea00-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "18/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "3204c720-1a5f-11e9-9061-8d8d70edf707",
        "Task_id": "9825ea00-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "19/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      }
    ]
  },
  {
    "Created_at": 1547491574168,
    "Frequency": {
      "wrapperName": "Set",
      "values": [
        7,
        4,
        3,
        2,
        1
      ],
      "type": "Number"
    },
    "IsActive": true,
    "Description": "New habit 4",
    "Color": "#912AD6",
    "Task_id": "ab378180-182c-11e9-957b-79c872c75fe1",
    "Username": "AndreaLovati",
    "completed": [
      {
        "Completed_task_id": "31d97160-1a5f-11e9-83fe-19bc94478832",
        "Task_id": "ab378180-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "02/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "31d66420-1a5f-11e9-9704-4d0fbbc1f59d",
        "Task_id": "ab378180-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "03/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "320f0050-1a5f-11e9-b48d-1f1c58f69181",
        "Task_id": "ab378180-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "07/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "3294d220-1a5f-11e9-85cd-737db15f755b",
        "Task_id": "ab378180-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "08/01/2019, 00:00:00",
        "Completed": true,
        "Username": "AndreaLovati"
      },
      {
        "Completed_task_id": "3323a4a0-1a5f-11e9-9e96-63fd5952ec24",
        "Task_id": "ab378180-182c-11e9-957b-79c872c75fe1",
        "Completed_at": "09/01/2019, 00:00:00",
        "Completed": false,
        "Username": "AndreaLovati"
      },
 etc..

我尝试过的是这个

 this.formattedHabits = this.habitsComplete.map(item => ({
        activities: item.completed.map(activity => ({
          [activity.Completed_at]:{
            completed: activity.Completed,
            frequency: item.Frequency.values,
            description: item.Description,
            color: item.Color,
          }
          })),
      }))

此代码为我提供了我在问题中发布的第一个对象的输出。 我希望这可以澄清情况

1 个答案:

答案 0 :(得分:1)

您可以通过迭代$('select').select2(); var $eventSelect = $("#mycombo"); $eventSelect.on("select2:select", function (e) { var title = $("#select2-mycombo-container").attr("title"); var myurl = $('#mycombo option').filter(function () { return $(this).html() == title; }).attr("url"); $("#myimage").attr("src",myurl); }); 数组来缩小数组,并返回一个以日期为键的对象。

activities
var data = [{ activities: [{ "05/01/2019": { completed: true, frequency: [7, 6, 2, 1], description: "Walk 100km", color: "#E97825" } }, { "07/01/2019": { completed: true, frequency: [7, 6, 2, 1], description: "Walk 100km", color: "#E97825" } }, { "08/01/2019": { completed: true, frequency: [7, 6, 2, 1], description: "Walk 100km", color: "#E97825" } }, { "12/01/2019": { completed: true, frequency: [7, 6, 2, 1], description: "Walk 100km", color: "#E97825" } }, { "14/01/2019": { completed: false, frequency: [7, 6, 2, 1], description: "Walk 100km", color: "#E97825" } }, { "15/01/2019": { completed: true, frequency: [7, 6, 2, 1], description: "Walk 100km", color: "#E97825" } }, { "19/01/2019": { completed: true, frequency: [7, 6, 2, 1], description: "Walk 100km", color: "#E97825" } }, { "21/01/2019": { completed: false, frequency: [7, 6, 2, 1], description: "Walk 100km", color: "#E97825" } }] }, { activities: [{ "02/01/2019": { completed: true, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }, { "03/01/2019": { completed: true, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }, { "04/01/2019": { completed: false, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }, { "07/01/2019": { completed: false, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }, { "09/01/2019": { completed: true, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }, { "10/01/2019": { completed: false, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }, { "11/01/2019": { completed: false, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }, { "14/01/2019": { completed: false, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }, { "16/01/2019": { completed: true, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }, { "17/01/2019": { completed: false, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }, { "18/01/2019": { completed: true, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }, { "21/01/2019": { completed: true, frequency: [5, 4, 3, 1], description: "Drink 4lt Water", color: "#ED1E24" } }] }, { activities: [{ "02/01/2019": { completed: false, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "03/01/2019": { completed: true, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "04/01/2019": { completed: true, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "05/01/2019": { completed: true, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "07/01/2019": { completed: true, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "08/01/2019": { completed: false, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "09/01/2019": { completed: true, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "10/01/2019": { completed: true, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "11/01/2019": { completed: false, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "12/01/2019": { completed: true, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "14/01/2019": { completed: true, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "15/01/2019": { completed: false, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "16/01/2019": { completed: true, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "17/01/2019": { completed: false, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "18/01/2019": { completed: true, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "19/01/2019": { completed: false, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }, { "21/01/2019": { completed: false, frequency: [7, 6, 5, 4, 3, 2, 1], description: "Study 2", color: "#F4ED59" } }] }, { activities: [{ "03/01/2019": { completed: true, frequency: [7, 6, 5, 4], description: "Home drinking food2", color: "#00A651" } }, { "04/01/2019": { completed: true, frequency: [7, 6, 5, 4], description: "Home drinking food2", color: "#00A651" } }, { "05/01/2019": { completed: true, frequency: [7, 6, 5, 4], description: "Home drinking food2", color: "#00A651" } }, { "10/01/2019": { completed: true, frequency: [7, 6, 5, 4], description: "Home drinking food2", color: "#00A651" } }, { "11/01/2019": { completed: true, frequency: [7, 6, 5, 4], description: "Home drinking food2", color: "#00A651" } }] }],
    result = data.reduce((r, { activities }) => {
        activities.forEach(o => Object
            .entries(o)
            .forEach(([k, v]) => (r[k] = r[k] || []).push(v))
        );
        return r;
    }, {});

console.log(result);