简体   繁体   中英

How to compare if, 2 separate Objects/Array have one same element and concat 2 objects into one array, if there is matching element? Javascript

I have a promise.all that returns 2 objects. The common element in these 2 seperate objects is room . I want both object to be in 1 array, and if the room matches, I would like to concat the objects.

const urls = ['/room/*/userconfig', '/room/*/observation/latest'];
let requests = urls.map(url => axios.get(url)); 

Promise.all(requests)
  .then(res => {
        let patientObjArray = Object.values(res[0].data);
        let patientObservationsArray = Object.values(res[1].data);
  })


const patientObjArray = [
  {
    room: "room18",
    patient: "91911671-302b-47b5-a24d-d2a1fea548d6",
    hospitalNumber: "12"
  },
  {
    room: "room19",
    patient: "b793bbba-b2bb-4995-9247-bb4d4f2be65a",
    hospitalNumber: "13"
  },
  {
    room: "room20",
    patient: "a8e8efa1-ea00-4d32-b4b2-58727200b9b0",
    hospitalNumber: "14"
  },
  {
    room: "room21",
    patient: "asdadddaddada-b4badasd2-7272a00qb9b0",
    hospitalNumber: "15"
  }
];

// patientObservationsArray is data related to patientObjArray.room

const patientObservationsArray = [
  {
    observation: {
      room: "room18",
      timestamp: "10:00",
      patient: "91911671-302b-47b5-a24d-d2a1fea548d6"
    },
    comment: { value: "Ok" }
  },
  {
    observation: {
      room: "room19",
      timestamp: "11:00",
      patient: "b793bbba-b2bb-4995-9247-bb4d4f2be65a"
    },
    comment: { value: "Good" }
  },
  {
    observation: {
      room: "room20",
      timestamp: "12:00",
      patient: "a8e8efa1-ea00-4d32-b4b2-58727200b9b0"
    },
    comment: { value: "bad" }
  }
];

expected output

const combinedArray = [
  {
    room: "room18",
    patient: "91911671-302b-47b5-a24d-d2a1fea548d6",
    hospitalNumber: "12",
    timestamp: "10:00",
    comment: "Ok"
  },
  {
    room: "room19",
    patient: "b793bbba-b2bb-4995-9247-bb4d4f2be65a",
    hospitalNumber: "13",
    timestamp: "11:00",
    comment: "Good"
  },
  {
    room: "room20",
    patient: "a8e8efa1-ea00-4d32-b4b2-58727200b9b0",
    hospitalNumber: "14",
    timestamp: "12:00",
    comment: "Bad"
  },
  {
    room: "room21",
    patient: "asdadddaddada-b4badasd2-7272a00qb9b0",
    hospitalNumber: "15"
  }
];

Here is my solution. Please try this code.

 const mergedArray = patientObjArray.map(obj => { const matched = patientObservationsArray.find(obs => obs.observation.room === obj.room); if (!!matched) { return { ...obj, ...matched.observation, comment: matched.comment.value }; } else { return obj; } }); console.log(mergedArray);

You can first flatten your patientObservationsArray , then merge it with your patientObjArray and finally use reduce method to merge your array.

There are probably better ways of doing this, feel free to give me suggestions. This should solve your problem.

 const patientObjArray = [ { room: "room18", patient: "91911671-302b-47b5-a24d-d2a1fea548d6", hospitalNumber: "12" }, { room: "room19", patient: "b793bbba-b2bb-4995-9247-bb4d4f2be65a", hospitalNumber: "13" }, { room: "room20", patient: "a8e8efa1-ea00-4d32-b4b2-58727200b9b0", hospitalNumber: "14" }, { room: "room21", patient: "asdadddaddada-b4badasd2-7272a00qb9b0", hospitalNumber: "15" } ]; const patientObservationsArray = [ { observation: { room: "room18", timestamp: "10:00", patient: "91911671-302b-47b5-a24d-d2a1fea548d6" }, comment: { value: "Ok" } }, { observation: { room: "room19", timestamp: "11:00", patient: "b793bbba-b2bb-4995-9247-bb4d4f2be65a" }, comment: { value: "Good" } }, { observation: { room: "room20", timestamp: "12:00", patient: "a8e8efa1-ea00-4d32-b4b2-58727200b9b0" }, comment: { value: "bad" } } ]; const combinedArray = patientObservationsArray // flatten your patientObservationsArray .map(item => ({ ...item.observation, ...item.comment })) // concat to your patientObjArray .concat(patientObjArray) // use reduce method to merge your array .reduce((res, item) => { const { room } = item; const index = res.findIndex(i => i.room === room); if (index > -1) { // merge object if already exists res[index] = { ...res[index], ...item }; } else { // else push into your array res.push(item); } return res; }, []); console.log(combinedArray);


Here is a playcode plunkr for my suggestion: https://playcode.io/473861?tabs=script.js,preview,console

We simply loop through the first array, run every object through a matching function against the second array and add the objects into a new array. If a match is found the required fields are added.

I would suggest keeping the object structure similar just to make using the objects in new data easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM