簡體   English   中英

Typescript - 基於另一個創建 object

[英]Typescript - Create object based on another

我有以下 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"
      }
   ]
}

基於前面的 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"
         ]
      }
   }
}

為了能夠在屏幕上繪制這樣的東西:

在此處輸入圖像描述

我更新了問題以更具體。 基於以上,我有這段代碼:

        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++;
            }    
        }

我該怎么做? 謝謝,

  1. 遍歷響應
  2. 應用一些邏輯將返回值轉換為日期並推入新屬性
  3. 插入 html / DOM 操作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM