将一个数据结构转换为另一个数据结构JavaScript / TypeScript

时间:2017-04-28 02:30:18

标签: 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的新手。伙计们好吗?提前致谢

3 个答案:

答案 0 :(得分:1)

我已经删除了一些打字稿位,因为它们并不真正相关,因为我认为主要问题是解析ROWDATA变量的逻辑。看看下面的代码,我已经包含了一些注释来解释它是如何工作的。

&#13;
&#13;
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)
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我不确定你需要动态哪些部分是动态的,因为对我来说,看起来你正在手工挑选某些属性,并根据一个明显的模式任意命名一些东西。

但是无论如何,如果你想通过一个函数运行第一个数据集来获得第二个输出,你可以这样做:

maven-compiler-plugin

link:https://es6console.com/j219msmv/

答案 2 :(得分:0)

在这里找到解决方法。

对不起,我使用Ramda.js,但Ramda.js帮助我减少了代码量。

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


const formatRoom = 
  R.pipe(R.last, R.prepend("0"), R.takeLast(2), R.join(""), (n) => `Room ${n}`)

const makeClass = (classSuffix) =>
  R.pipe(R.prop('lesson'), R.split(" "), R.last, (name) => `lesson${name}${classSuffix}`,)

const spec = {
  class: R.prop('room'),
  uid: R.prop('id'),
  title: {
    field: R.always('lesson'),
    text: R.prop('lesson'),
    class: makeClass("-title"),
  },
  content: [
    {
      field: R.always('education_grade_subject'),
      text: R.prop('education_grade_subject'),
      class: makeClass("-subject-class"),
    },
    {
      field: R.always('staff'),
      text: R.prop('staff'),
      class: makeClass("-teacher-class"),
    },
    {
      field: R.always('room'),
      text: R.pipe(R.prop('room'), formatRoom),
      class: makeClass("-room-class"),
    },
  ],
}

const formatTime =
  R.dropLast(3)

const transformData =
  R.pipe(
    R.groupBy(R.prop('start_time')),
    R.map(R.groupBy(R.prop('day_of_week'))),
    R.map(R.map(R.map(R.applySpec(spec)))),
    R.toPairs,
    R.map(([start_time, groupsByDay]) =>
      R.assoc('time', formatTime(start_time), groupsByDay)),
  )

console.log(transformData(ROWDATA))
<script src="//cdn.jsdelivr.net/npm/ramda@0.25.0/dist/ramda.min.js"></script>