gpt4 book ai didi

javascript - 将一种数据结构转换为另一种数据结构 JavaScript/TypeScript

转载 作者:行者123 更新时间:2023-12-03 04:26:55 25 4
gpt4 key购买 nike

嗨,我有一个行数据,如下所示:

export const ROWDATA: Array<any> = [
{
id: "1",
start_time: "9:00:00",
end_time: "9:30:00",
day_of_week: 'monday',
lesson: "Lesson ABC",
class: "Class ABC",
room: "room1",
education_grade_subject: "Physics",
staff: "Amanda Jeremy",
timetable: "class timetable",
modified_user: "admin",
modified: "2017-01-15",
created_user: "admin",
created: "2017-01-15"
},
{
id: "2",
start_time: "9:30:00",
end_time: "10:00:00",
day_of_week: 'monday',
lesson: "Lesson XYZ",
class: "Class ABC",
room: "room2",
education_grade_subject: "Chemistry",
staff: "Amanda Jeremy",
timetable: "class timetable",
modified_user: "admin",
modified: "2017-01-15",
created_user: "admin",
created: "2017-01-15"
},
.....

];

基本上它是我网站中表格的行数据结构。我从后端检索的行数据。现在我需要将此数据转换为我网站的另一种格式。新格式需要采用以下格式:

export const DATA: Array<any> = [
{
time: "9:00",
monday: [
{
class: 'room1',
title: {field: "lesson", text:"Lesson ABC", class:"lessonABC-title"},
content: [
{field: "education_grade_subject", text: "Physics", class:"lessonABC-subject-class"},
{field: "staff", text: "Amanda Jeremy", class:"lessonABC-staff-class"},
{field: "room", text: "Room 01", class:"lessonABC-room-class"}
],
uid: "1"
}
]
},

{
time: "9:30",
monday: [
{class: 'room2',
title: {field: "lesson", text:"Lesson XYZ", class:"lessonXYZ-title"},
content: [
{field: "education_grade_subject", text: "Chemistry", class:"lessonXYZ-subject-class"},
{field: "staff", text: "Amanda Jeremy", class:"lessonXYZ-teacher-class"},
{field: "room", text: "Room 02", class:"lessonXYZ-room-class"}
],
uid: "2"
}
]
},
....

第二数据或纯粹基于第一数据转换。我怎样才能实现这些?对于 JavaScript 来说非常陌生。大家有什么想法吗?提前致谢

最佳答案

我在这里删除了一些 typescript 位,因为它们并不真正相关,因为我认为主要问题是解析 ROWDATA 变量的逻辑。看一下下面的代码,我添加了一些注释来解释它是如何工作的。

const ROWDATA = [
{
id: "1",
start_time: "9:00:00",
end_time: "9:30:00",
day_of_week: 'monday',
lesson: "Lesson ABC",
class: "Class ABC",
room: "room1",
education_grade_subject: "Physics",
staff: "Amanda Jeremy",
timetable: "class timetable",
modified_user: "admin",
modified: "2017-01-15",
created_user: "admin",
created: "2017-01-15"
},
{
id: "2",
start_time: "9:30:00",
end_time: "10:00:00",
day_of_week: 'monday',
lesson: "Lesson XYZ",
class: "Class ABC",
room: "room2",
education_grade_subject: "Chemistry",
staff: "Amanda Jeremy",
timetable: "class timetable",
modified_user: "admin",
modified: "2017-01-15",
created_user: "admin",
created: "2017-01-15"
},
];

const group = [];
const map ={}; // keep track of the time that we have seen
ROWDATA.forEach(e => {
// object to be inserted to the day array in each of the object containing
// time property
const newClass = {
class: e.class
// ... fill in the rest of info here...
}

// if time does not exist in map,
// create new entry
if (!map[e.start_time]) {
map[e.start_time] = true;
const newEntry = { time: e.start_time };
newEntry[e.day_of_week] = [];
newEntry[e.day_of_week].push(newClass);
group.push(newEntry);
} else {
// time already exist, find that object with corresponding time
// and push the new class to the respective day
group.map(e2 => {
if (e.start_time === e2.time) {
if (e2[e.day_of_week]) {
e2[e.day_of_week].push(newClass);
} else {
console.log('!!!')
e2[e.day_of_week] = [];
e2[e.day_of_week].push(newClass);
}
}
return e2;
});
}
});

console.log(group)

关于javascript - 将一种数据结构转换为另一种数据结构 JavaScript/TypeScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com