Home > OS >  Js/Typescript return values in array a and not in array b
Js/Typescript return values in array a and not in array b

Time:11-16

What is a quick way to determine items in array A that are not in array B? I have tried this :

const url = `/ws/rest/v1/visit?includeInactive=false&v=${customRepresentation}`;
const {
    data: visitsData,
    error: visitsError,
    isValidating: visitsIsValidating,
  } = useSWR<{ data: { results: Array<Visit> } }, Error>(sessionLocation ? url : null, openmrsFetch);

const apiUrl = `/ws/rest/v1/visit-queue-entry?v=full`;
const {
    data: queueData,
    error: queueError,
    isValidating: queueIsValidating,
  } = useSWR<{ data: { results: Array<VisitQueueEntry> } }, Error>(apiUrl, openmrsFetch);

const data = visitsData?.data?.results?.filter(
    (o, i) => o?.patient?.uuid !== queueData?.data?.results[i]?.queueEntry.patient?.uuid,
  );

Unfortunately i keep getting the value of visitsData. When i try using a different array, i get the correct output. This is the structure of queueData :

export const mockVisitQueueEntries = [
  {
    uuid: 'fa1e98f1-f002-4174-9e55-34d60951e710',
    visit: {
      uuid: 'c90386ff-ae85-45cc-8a01-25852099c5ae',
      display: 'Facility Visit @ Outpatient Clinic - 04/03/2022 07:22',
    },
    queueEntry: {
      uuid: '712289ab-32c0-430f-87b6-d9c1e4e4686e',
      display: 'Eric Test Ric',
      priorityComment: 'Needs Triage',
      sortWeight: 0,
      startedAt: '2022-03-04T09:50:54.000 0000',
      endedAt: null,
      queue: {
        uuid: '6a97bd65-3a9a-4fab-ae8f-be59dd4ddd87',
        display: 'TRIAGE QUEUE',
        name: 'TRIAGE QUEUE',
        description: 'Queue for patients waiting for triage',
        service: {
          display: 'Triage',
        },
      },
      status: {
        uuid: 'aaec62b1-4b03-4166-ada7-230cb4b4aaaa',
        display: 'Waiting',
        links: [
          {
            rel: 'self',
            uri: 'http://openmrs:8080/openmrs/ws/rest/v1/concept/aaec62b1-4b03-4166-ada7-230cb4b4aaaa',
          },
        ],
      },
      patient: {
        uuid: 'cc75ad73-c24b-499c-8db9-a7ef4fc0b36d',
        display: '10000F1 - Eric Test Ric',
        person: {
          age: '32',
          gender: 'F',
        },
      },
      priority: {
        uuid: 'f9684018-a4d3-4d6f-9dd5-b4b1e89af3e7',
        display: 'Not Urgent',
      },
      locationWaitingFor: null,
      providerWaitingFor: null,
    },
  },
  {
    uuid: '2f85d611-5bb9-4bca-b6f8-661517df86c9',
    visit: {
      uuid: '6b3e233d-2b44-40ca-b0c8-c5a57a8c51b6',
      display: 'Home Visit @ Outpatient Clinic - 09/03/2022 21:08',
    },
    queueEntry: {
      uuid: '5f017eb0-b035-4acd-b284-da45f5067502',
      display: 'John Smith',
      priorityComment: 'Needs immediate assistance',
      sortWeight: 0,
      startedAt: '2022-03-09T13:50:54.000 0000',
      endedAt: null,
      queue: {
        uuid: 'c187d78b-5c54-49bf-a0f8-b7fb6034d36d',
        display: 'Consultation queue',
        name: 'Consultation queue',
        description: 'A queue for patients for a clincal consultation i.e. Doctor, Clinician',
        service: {
          display: 'Clinical Consultation',
        },
      },
      status: {
        uuid: 'aaec62b1-4b03-4166-ada7-230cb4b4aaaa',
        display: 'Waiting',
      },
      patient: {
        uuid: '53568469-f652-470d-95e8-13131914286b',
        display: '10000JT - John Smith',
        person: {
          age: '27',
          gender: 'M',
        },
      },
      priority: {
        uuid: 'b6a84ad0-c5e6-4a37-896e-5b7a0bccfd6c',
        display: 'Emergency',
      },
      locationWaitingFor: null,
      providerWaitingFor: null,
    },
  },
];

This is the structure of Data :

{
  "results": [
    {
      "uuid": "403a040c-88a0-4ac0-a5be-ec5c9bd977d3",
      "patient": {
        "uuid": "cd3f8076-3eb0-460b-b6c1-c9b7db079848",
        "identifiers": [
          {
            "identifier": "MH4NC7",
            "uuid": "7e56a13b-7738-4cd4-9082-736dd4ca5706"
          }
        ],
        "person": {
          "age": 77,
          "display": "syokau ann ann",
          "gender": "F",
          "uuid": "cd3f8076-3eb0-460b-b6c1-c9b7db079848"
        }
      },
      "visitType": {
        "uuid": "3371a4d4-f66f-4454-a86d-92c7b3da990c",
        "name": "Outpatient",
        "display": "Outpatient"
      },
      "location": {
        "uuid": "6d90fea0-052f-4494-a4a1-0d9d02b43d27",
        "name": "10 Engineer VCT",
        "display": "10 Engineer VCT"
      },
      "startDatetime": "2022-11-15T17:43:00.000 0300",
      "stopDatetime": null
    },
    {
      "uuid": "ca8f7c3f-000f-4b5d-bac8-efe33199233e",
      "patient": {
        "uuid": "13032ec9-9741-47ff-a046-d5e78ff65c7a",
        "identifiers": [
          {
            "identifier": "MJ4FMM",
            "uuid": "9a10df3b-ba90-4359-a530-df42a10fc170"
          }
        ],
        "person": {
          "age": 26,
          "display": "mbithe mulei nelius",
          "gender": "F",
          "uuid": "13032ec9-9741-47ff-a046-d5e78ff65c7a"
        }
      },
      "visitType": {
        "uuid": "3371a4d4-f66f-4454-a86d-92c7b3da990c",
        "name": "Outpatient",
        "display": "Outpatient"
      },
      "location": {
        "uuid": "6d90fea0-052f-4494-a4a1-0d9d02b43d27",
        "name": "10 Engineer VCT",
        "display": "10 Engineer VCT"
      },
      "startDatetime": "2022-11-15T16:25:00.000 0300",
      "stopDatetime": null
    }
  ]
}

What could i be doing wrong? Any advice/recommendations will be appreciated.

CodePudding user response:

  1. Not optimal way

by iterating over queueData.data.results

const data = visitsData?.data?.results?.filter((o) => {
  if (queueData?.data?.results.find((item) => item?.queueEntry.patient?.uuid === o?.patient?.uuid) { return false }
  return true
});
  1. Optimal way by using an object where key would be item?.queueEntry.patient?.uuid
const byId = {}
queueData?.data?.results.forEach((item) => {
  if (item?.queueEntry.patient?.uuid) {
    byId[item?.queueEntry.patient?.uuid] = true
  }
});

const data = visitsData?.data?.results?.filter((item) => {
  if (byId[item?.patient?.uuid]) { return false }
  return true
});
  • Related