Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const util = require("util");
- const input = [
- {
- id: 2,
- first_name: "Javier",
- last_name: "Sotomayor",
- email: "jsotomayor@gmail.com",
- profession: "Endocrinologist",
- "appointment.id": 1,
- "appointment.patient_id": 1,
- "appointment.doctor_operation_time_id": 1,
- "appointment.height": null,
- "appointment.weight": null,
- "appointment.glucotest": null,
- "appointment.reason": "No reason",
- "appointment.notes": "Some notes",
- "appointment.dot.id": 1,
- "appointment.dot.doctor_id": 2,
- "appointment.dot.operation_time_id": 1,
- "appointment.dot.operation_time.id": 1,
- "appointment.dot.operation_time.start": "2018-06-25T13:00:00.000Z",
- "appointment.dot.operation_time.end": "2018-06-25T14:00:00.000Z"
- },
- {
- id: 2,
- first_name: "Javier",
- last_name: "Sotomayor",
- email: "jsotomayor@gmail.com",
- profession: "Endocrinologist",
- "appointment.id": 2,
- "appointment.patient_id": 1,
- "appointment.doctor_operation_time_id": 2,
- "appointment.height": null,
- "appointment.weight": null,
- "appointment.glucotest": null,
- "appointment.reason": "No reason",
- "appointment.notes": "Some notes",
- "appointment.dot.id": 2,
- "appointment.dot.doctor_id": 2,
- "appointment.dot.operation_time_id": 3,
- "appointment.dot.operation_time.id": 3,
- "appointment.dot.operation_time.start": "2018-06-25T16:00:00.000Z",
- "appointment.dot.operation_time.end": "2018-06-25T17:00:00.000Z"
- },
- {
- id: 2,
- first_name: "Javier",
- last_name: "Sotomayor",
- email: "jsotomayor@gmail.com",
- profession: "Endocrinologist",
- "appointment.id": 7,
- "appointment.patient_id": 1,
- "appointment.doctor_operation_time_id": 6,
- "appointment.height": null,
- "appointment.weight": null,
- "appointment.glucotest": null,
- "appointment.reason": "Body health control",
- "appointment.notes": "Some notes for appointment with patient_id =1",
- "appointment.dot.id": 6,
- "appointment.dot.doctor_id": 2,
- "appointment.dot.operation_time_id": 19,
- "appointment.dot.operation_time.id": 19,
- "appointment.dot.operation_time.start": "2018-06-27T16:00:00.000Z",
- "appointment.dot.operation_time.end": "2018-06-27T17:00:00.000Z"
- },
- {
- id: 2,
- first_name: "Javier",
- last_name: "Sotomayor",
- email: "jsotomayor@gmail.com",
- profession: "Endocrinologist",
- "appointment.id": 8,
- "appointment.patient_id": 1,
- "appointment.doctor_operation_time_id": 7,
- "appointment.height": null,
- "appointment.weight": null,
- "appointment.glucotest": null,
- "appointment.reason": "Body health control",
- "appointment.notes": "Some notes for appointment with patient_id =1",
- "appointment.dot.id": 7,
- "appointment.dot.doctor_id": 2,
- "appointment.dot.operation_time_id": 25,
- "appointment.dot.operation_time.id": 25,
- "appointment.dot.operation_time.start": "2018-06-28T13:00:00.000Z",
- "appointment.dot.operation_time.end": "2018-06-28T14:00:00.000Z"
- }
- ];
- function parser(json) {
- let newObject = {};
- Object.keys(json).forEach(key => {
- const newKeys = key.split(".");
- const lastKey = newKeys.pop();
- let pointer = newObject;
- newKeys.forEach(newKey => {
- if (pointer[newKey] === undefined) pointer[newKey] = {};
- pointer = pointer[newKey];
- });
- pointer[lastKey] = json[key];
- });
- return newObject;
- }
- const output = input.map(i => parser(i));
- console.log("[INPUT]: ", util.inspect(input, false, null));
- console.log("[OUTPUT]: ", util.inspect(output, false, null));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement