{ 
   "workRequestCompleted":[ 
      { 
         "number":"4329502",
         "name":"Scheduled Outage Primary Feeder",
         "startDate":"2019-07-01",
         "endDate":"2019-07-10",
         "lastUpdate":"2019-12-01T05:00:00Z",
         "contractId":null,
         "status":"Scheduled"
      }
   ],
   "prerequisites":[ 
      { 
         "preq_id":"M12345",
         "preq_type":"FLAG",
         "preq_date_time":"2019-10-02T03:28:00Z",
         "preq_last_update":"2018-12-03T13:30:00Z",
         "preq_crew_id_id":"W_FOD_013",
         "wr_number":"4329502",
         "preq_crew":{ 
            "crew_id":"W_FOD_013",
            "crew_name":"Pre-req 1",
            "expertise":"Flush"
         },
         "facilities":[ 
            { 
               "facility_id":"VS1234",
               "facility_type":"VAULT",
               "facility_lat":40.758896,
               "facility_long":-73.98513,
               "facility_notes":"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            }
         ]
      }
   ],
   "workComponents":[ 
      { 
         "wc_number":"888-FOD-CO-950-609",
         "wc_type":"PSMECH",
         "wc_description":"Make halves, pick up VS1234.",
         "wc_crew_id":"W_FOD_013",
         "wc_start_date_time":null,
         "wc_end_date_time":null,
         "wc_estimated_hours":10.0,
         "wc_scheduled_start_date_time":"2020-02-16T19:23:02.866019Z",
         "wc_scheduled_end_date_time":"2020-02-17T19:23:02.866028Z",
         "wc_actual_hours":0.0,
         "wc_status":"Scheduled",
         "wc_notes":null,
         "wc_number_pre":"888-FOD-CO-178-0",
         "wc_number_post":"",
         "delay_type":null,
         "wr_number":"4329502",
         "wc_crew":{ 
            "crew_id":"W_FOD_013",
            "crew_name":"Pre-req 1",
            "expertise":"Flush"
         },
         "facilities":[ 
            { 
               "facility_id":"VS1234",
               "facility_type":"VAULT",
               "facility_lat":40.758896,
               "facility_long":-73.98513,
               "facility_notes":"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            }
         ]
      },
      { 
         "wc_number":"888-FOD-CO-178-0",
         "wc_type":"PSMECH",
         "wc_description":"Make halves, pick up VS1234.",
         "wc_crew_id":"W_FOD_013",
         "wc_start_date_time":"2019-12-02T04:28:00Z",
         "wc_end_date_time":"2019-12-03T13:30:00Z",
         "wc_estimated_hours":10.0,
         "wc_scheduled_start_date_time":null,
         "wc_scheduled_end_date_time":null,
         "wc_actual_hours":10.0,
         "wc_status":"Pending\n",
         "wc_notes":"We noticed that VS5678 may require additional maintenance soon.",
         "wc_number_pre":"",
         "wc_number_post":"",
         "delay_type":"",
         "wr_number":"4329502",
         "wc_crew":{ 
            "crew_id":"W_FOD_013",
            "crew_name":"Pre-req 1",
            "expertise":"Flush"
         },
         "facilities":[ 
            { 
               "facility_id":"VS1234",
               "facility_type":"VAULT",
               "facility_lat":40.758896,
               "facility_long":-73.98513,
               "facility_notes":"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            }
         ]
      }
   ]
} 

J'ai ce JSON qui contient un tableau, je souhaite combiner un tableau d'installations ayant le même identifiant d'installation à partir du tableau Prerequisites et WorkComponents et créer un nouveau tableau contenant l'ID d'installation et preq_type avec wc_type séparé par une virgule s'il a le même identifiant d'installation, comment puis-je faire en javascript ?, merci

Résultat attendu :

[
        {
            "facility_id": "VS1234",
            "type": ["FLAG", "PSMECH", "PSMECH"]
        }
]
-2
user3567523 17 févr. 2020 à 07:40

1 réponse

Meilleure réponse

Vous pouvez utiliser plusieurs boucles forEach et créer un objet.

const data = {
  workRequestCompleted: [
    {
      number: "4329502",
      name: "Scheduled Outage Primary Feeder",
      startDate: "2019-07-01",
      endDate: "2019-07-10",
      lastUpdate: "2019-12-01T05:00:00Z",
      contractId: null,
      status: "Scheduled"
    }
  ],
  prerequisites: [
    {
      preq_id: "M12345",
      preq_type: "FLAG",
      preq_date_time: "2019-10-02T03:28:00Z",
      preq_last_update: "2018-12-03T13:30:00Z",
      preq_crew_id_id: "W_FOD_013",
      wr_number: "4329502",
      preq_crew: {
        crew_id: "W_FOD_013",
        crew_name: "Pre-req 1",
        expertise: "Flush"
      },
      facilities: [
        {
          facility_id: "VS1234",
          facility_type: "VAULT",
          facility_lat: 40.758896,
          facility_long: -73.98513,
          facility_notes:
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        }
      ]
    }
  ],
  workComponents: [
    {
      wc_number: "888-FOD-CO-950-609",
      wc_type: "PSMECH",
      wc_description: "Make halves, pick up VS1234.",
      wc_crew_id: "W_FOD_013",
      wc_start_date_time: null,
      wc_end_date_time: null,
      wc_estimated_hours: 10.0,
      wc_scheduled_start_date_time: "2020-02-16T19:23:02.866019Z",
      wc_scheduled_end_date_time: "2020-02-17T19:23:02.866028Z",
      wc_actual_hours: 0.0,
      wc_status: "Scheduled",
      wc_notes: null,
      wc_number_pre: "888-FOD-CO-178-0",
      wc_number_post: "",
      delay_type: null,
      wr_number: "4329502",
      wc_crew: {
        crew_id: "W_FOD_013",
        crew_name: "Pre-req 1",
        expertise: "Flush"
      },
      facilities: [
        {
          facility_id: "VS1234",
          facility_type: "VAULT",
          facility_lat: 40.758896,
          facility_long: -73.98513,
          facility_notes:
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        }
      ]
    },
    {
      wc_number: "888-FOD-CO-178-0",
      wc_type: "PSMECH",
      wc_description: "Make halves, pick up VS1234.",
      wc_crew_id: "W_FOD_013",
      wc_start_date_time: "2019-12-02T04:28:00Z",
      wc_end_date_time: "2019-12-03T13:30:00Z",
      wc_estimated_hours: 10.0,
      wc_scheduled_start_date_time: null,
      wc_scheduled_end_date_time: null,
      wc_actual_hours: 10.0,
      wc_status: "Pending\n",
      wc_notes:
        "We noticed that VS5678 may require additional maintenance soon.",
      wc_number_pre: "",
      wc_number_post: "",
      delay_type: "",
      wr_number: "4329502",
      wc_crew: {
        crew_id: "W_FOD_013",
        crew_name: "Pre-req 1",
        expertise: "Flush"
      },
      facilities: [
        {
          facility_id: "VS1234",
          facility_type: "VAULT",
          facility_lat: 40.758896,
          facility_long: -73.98513,
          facility_notes:
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        }
      ]
    }
  ]
};

const res = {};

data.prerequisites.forEach(pre => {
  pre.facilities.forEach(fac => {
    res[fac.facility_id] =
      fac.facility_id in res
        ? {
            ...res[fac.facility_id],
            type: [...res[fac.facility_id].type, pre.preq_type]
          }
        : { facility_id: fac.facility_id, type: [pre.preq_type] };
  });
});

data.workComponents.forEach(work => {
  work.facilities.forEach(fac => {
    res[fac.facility_id] =
      fac.facility_id in res
        ? {
            ...res[fac.facility_id],
            type: [...res[fac.facility_id].type, work.wc_type]
          }
        : { facility_id: fac.facility_id, type: [work.wc_type] };
  });
});

const required_data = Object.values(res);

console.log(required_data);
0
Siva K V 17 févr. 2020 à 05:17