在打字稿中访问json数据中的内部键

问题描述 投票:0回答:2

我有以下对象

{
    "Monday": [
        {
            "morning": [
                {
                    "start_time": "02:00",
                    "end_time": "07:30"
                }
            ],
            "afternoon": [
                {
                    "start_time": "02:00",
                    "end_time": "05:00"
                }
            ],
            "evening": [
                {
                    "start_time": "02:30",
                    "end_time": "07:00"
                }
            ]
        }
    ],
    "Tuesday": [
        {
            "morning": [
                {
                    "start_time": "02:00",
                    "end_time": "07:30"
                }
            ],
            "afternoon": [
                {
                    "start_time": "02:00",
                    "end_time": "05:00"
                }
            ],
            "evening": [
                {
                    "start_time": "02:30",
                    "end_time": "07:00"
                }
            ]
        }
    ],
..
}

我想循环遍历所有对象键和值

for (var prop in this.jsonData) {
  console.log("Key:" + prop);
  console.log("Value:" + this.jsonData[prop]);
}

得到

键:周一值:未定义

但我需要访问内部对象值

使用打字稿进行笔记

javascript json object key
2个回答
0
投票

您还需要循环访问内部数组才能访问内部对象值。基本上,这可以通过以下方式实现:

  • Object.entries()
    获取外部和内部对象的键值对数组。
  • 迭代数组,并且
  • 解构以直接从对象中提取值。
Object.entries(this.jsonData).forEach(([day, dayData]) => {
  console.log("Day:", day);
  const timePeriods = dayData[0];

  Object.entries(timePeriods).forEach(([timePeriod, slots]) => {
    console.log("  Time Period:", timePeriod);

    slots.forEach(({ start_time, end_time }, i) => {
      console.log(`    Slot ${i + 1}`);
      console.log("      Start Time:", start_time);
      console.log("      End Time:", end_time);
    });
  });
});

const jsonData = {
  Monday: [
    {
      morning: [
        {
          start_time: "02:00",
          end_time: "07:30",
        },
      ],
      afternoon: [
        {
          start_time: "02:00",
          end_time: "05:00",
        },
      ],
      evening: [
        {
          start_time: "02:30",
          end_time: "07:00",
        },
      ],
    },
  ],
  Tuesday: [
    {
      morning: [
        {
          start_time: "02:00",
          end_time: "07:30",
        },
      ],
      afternoon: [
        {
          start_time: "02:00",
          end_time: "05:00",
        },
      ],
      evening: [
        {
          start_time: "02:30",
          end_time: "07:00",
        },
      ],
    },
  ],
};

Object.entries(jsonData).forEach(([day, dayData]) => {
  console.log("Day:", day);
  const timePeriods = dayData[0];

  Object.entries(timePeriods).forEach(([timePeriod, slots]) => {
    console.log("  Time Period:", timePeriod);

    slots.forEach(({ start_time, end_time }, i) => {
      console.log(`    Slot ${i + 1}`);
      console.log("      Start Time:", start_time);
      console.log("      End Time:", end_time);
    });
  });
});


0
投票

嵌套对象访问

我们可以看到对象结构相当复杂,我们需要深入所有子对象,即日期,然后是时间。

为此,我们可以编写一个函数:

this.jsonData = {
    "Monday": [
        {
            "morning": [
                {
                    "start_time": "02:00",
                    "end_time": "07:30"
                }
            ],
            "afternoon": [
                {
                    "start_time": "02:00",
                    "end_time": "05:00"
                }
            ],
            "evening": [
                {
                    "start_time": "02:30",
                    "end_time": "07:00"
                }
            ]
        }
    ],
    "Tuesday": [
        {
            "morning": [
                {
                    "start_time": "02:00",
                    "end_time": "07:30"
                }
            ],
            "afternoon": [
                {
                    "start_time": "02:00",
                    "end_time": "05:00"
                }
            ],
            "evening": [
                {
                    "start_time": "02:30",
                    "end_time": "07:00"
                }
            ]
        }
    ],
}


for (var day in this.jsonData) {
  console.log("Day:" + day);
  
  // Accessing the array of schedules for the current day
  var schedules = this.jsonData[day];
  
  // Looping through the array of schedules
  for (var i = 0; i < schedules.length; i++) {
    var schedule = schedules[i];
    
    // Looping through the keys (morning, afternoon, evening)
    for (var timeOfDay in schedule) {
      console.log("Time of day: " + timeOfDay);
      
      // Accessing the array of time slots for the current time of day
      var timeSlots = schedule[timeOfDay];
      
      // Looping through the array of time slots
      for (var j = 0; j < timeSlots.length; j++) {
        var timeSlot = timeSlots[j];
        
        console.log("Start time: " + timeSlot.start_time);
        console.log("End time: " + timeSlot.end_time);
      }
    }
  }
}

Ps:您可以添加搜索键,查找特定的日期或时间,而不是记录所有内容,您也可以将这些值保存在某处。

© www.soinside.com 2019 - 2024. All rights reserved.