Advertisement
Guest User

Untitled

a guest
Jun 30th, 2018
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const util = require("util");
  2.  
  3. const input = [
  4.   {
  5.     id: 2,
  6.     first_name: "Javier",
  7.     last_name: "Sotomayor",
  8.     email: "jsotomayor@gmail.com",
  9.     profession: "Endocrinologist",
  10.     "appointment.id": 1,
  11.     "appointment.patient_id": 1,
  12.     "appointment.doctor_operation_time_id": 1,
  13.     "appointment.height": null,
  14.     "appointment.weight": null,
  15.     "appointment.glucotest": null,
  16.     "appointment.reason": "No reason",
  17.     "appointment.notes": "Some notes",
  18.     "appointment.dot.id": 1,
  19.     "appointment.dot.doctor_id": 2,
  20.     "appointment.dot.operation_time_id": 1,
  21.     "appointment.dot.operation_time.id": 1,
  22.     "appointment.dot.operation_time.start": "2018-06-25T13:00:00.000Z",
  23.     "appointment.dot.operation_time.end": "2018-06-25T14:00:00.000Z"
  24.   },
  25.   {
  26.     id: 2,
  27.     first_name: "Javier",
  28.     last_name: "Sotomayor",
  29.     email: "jsotomayor@gmail.com",
  30.     profession: "Endocrinologist",
  31.     "appointment.id": 2,
  32.     "appointment.patient_id": 1,
  33.     "appointment.doctor_operation_time_id": 2,
  34.     "appointment.height": null,
  35.     "appointment.weight": null,
  36.     "appointment.glucotest": null,
  37.     "appointment.reason": "No reason",
  38.     "appointment.notes": "Some notes",
  39.     "appointment.dot.id": 2,
  40.     "appointment.dot.doctor_id": 2,
  41.     "appointment.dot.operation_time_id": 3,
  42.     "appointment.dot.operation_time.id": 3,
  43.     "appointment.dot.operation_time.start": "2018-06-25T16:00:00.000Z",
  44.     "appointment.dot.operation_time.end": "2018-06-25T17:00:00.000Z"
  45.   },
  46.   {
  47.     id: 2,
  48.     first_name: "Javier",
  49.     last_name: "Sotomayor",
  50.     email: "jsotomayor@gmail.com",
  51.     profession: "Endocrinologist",
  52.     "appointment.id": 7,
  53.     "appointment.patient_id": 1,
  54.     "appointment.doctor_operation_time_id": 6,
  55.     "appointment.height": null,
  56.     "appointment.weight": null,
  57.     "appointment.glucotest": null,
  58.     "appointment.reason": "Body health control",
  59.     "appointment.notes": "Some notes for appointment with patient_id =1",
  60.     "appointment.dot.id": 6,
  61.     "appointment.dot.doctor_id": 2,
  62.     "appointment.dot.operation_time_id": 19,
  63.     "appointment.dot.operation_time.id": 19,
  64.     "appointment.dot.operation_time.start": "2018-06-27T16:00:00.000Z",
  65.     "appointment.dot.operation_time.end": "2018-06-27T17:00:00.000Z"
  66.   },
  67.   {
  68.     id: 2,
  69.     first_name: "Javier",
  70.     last_name: "Sotomayor",
  71.     email: "jsotomayor@gmail.com",
  72.     profession: "Endocrinologist",
  73.     "appointment.id": 8,
  74.     "appointment.patient_id": 1,
  75.     "appointment.doctor_operation_time_id": 7,
  76.     "appointment.height": null,
  77.     "appointment.weight": null,
  78.     "appointment.glucotest": null,
  79.     "appointment.reason": "Body health control",
  80.     "appointment.notes": "Some notes for appointment with patient_id =1",
  81.     "appointment.dot.id": 7,
  82.     "appointment.dot.doctor_id": 2,
  83.     "appointment.dot.operation_time_id": 25,
  84.     "appointment.dot.operation_time.id": 25,
  85.     "appointment.dot.operation_time.start": "2018-06-28T13:00:00.000Z",
  86.     "appointment.dot.operation_time.end": "2018-06-28T14:00:00.000Z"
  87.   }
  88. ];
  89.  
  90. function parser(json) {
  91.   let newObject = {};
  92.   Object.keys(json).forEach(key => {
  93.     const newKeys = key.split(".");
  94.     const lastKey = newKeys.pop();
  95.     let pointer = newObject;
  96.     newKeys.forEach(newKey => {
  97.       if (pointer[newKey] === undefined) pointer[newKey] = {};
  98.       pointer = pointer[newKey];
  99.     });
  100.     pointer[lastKey] = json[key];
  101.   });
  102.   return newObject;
  103. }
  104.  
  105. const output = input.map(i => parser(i));
  106.  
  107. console.log("[INPUT]: ", util.inspect(input, false, null));
  108. console.log("[OUTPUT]: ", util.inspect(output, false, null));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement