简体   繁体   English

Typescript - 基于另一个创建 object

[英]Typescript - Create object based on another

I have the following object (this is the service response):我有以下 object(这是服务响应):

let contentArray = {
   "errorMessages":[
      
   ],
   "output":[
      {
         "id":1,
         "excecuteDate":"2022-02-04T13:34:20"
      },
      {
         "id":2,
         "excecuteDate":"2022-02-04T12:04:20"
      },
      {
         "id":3,
         "excecuteDate":"2022-02-01T11:23:34"
      },
      {
         "id":4,
         "excecuteDate":"2022-01-14T10:30:54"
      },
      {
         "id":5,
         "excecuteDate":"2021-11-03T10:20:43"
      }
   ]
}

Based on the previous object I have to create the following:基于前面的 object 我必须创建以下内容:

{
   "2021":{
      "11-November":{
         "03/11/2021":[
            "10:20:43"
         ]
      }
   },
   "2022":{
      "01_January":{
         "14/01/2022":[
            "10:30:54"
         ]
      },
      "02_February":{
         "01/02/2022":[
            "11:23:34"
         ],
         "04/02/2022":[
            "12:04:20", "13:34:20"
         ]
      }
   }
}

To be able to paint something like this on the screen:为了能够在屏幕上绘制这样的东西:

在此处输入图像描述

I updated the question to be more specific.我更新了问题以更具体。 Based on the above, I have this code:基于以上,我有这段代码:

        let dateExecutionValue: Date;
        let i: number = 0;
        let tree = {};
        while (i < this.contentArray.length) {
            dateExecutionValue = new Date(this.contentArray[i].excecuteDate);
            let year = dateExecutionValue.getFullYear();
            tree[year] = {};
            let auxYear = year;
            while (auxYear === year && i < this.contentArray.length) {
                let month = dateExecutionValue.getMonth() + 1;
                // here, now under the year property of the object, I want to create another month property (for each month associated with that year)
                i++;
            }    
        }

How could I do it?我该怎么做? thanks,谢谢,

  1. Iterate through the response遍历响应
  2. Apply some logic to transform returned value into a date and push into new property应用一些逻辑将返回值转换为日期并推入新属性
  3. Insert into html / DOM Manipulation插入 html / DOM 操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM