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

标签 javascript angular typescript

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

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/

相关文章:

javascript - jQuery $.get 将 PHP 代码作为文本而不是值返回

javascript - 如何在 Protractor 中的元素上鼠标移动

Angular 4 FormGroup removeControl() 不更新表单

angular - 如何从可观察对象数组中返回可观察对象

node.js - 如何将 DynamoDB Streams 对象映射到 Javascript 对象?

javascript - 查询数据库并在同一页面显示结果

javascript - 如何在 jQuery 的对象中指定带有 on 事件组合的目标元素

c# - Angular 2 和 C#/.NET

javascript - 类型安全的嵌套属性查找

javascript - Sequelize : SQL injection in findOne?