1

I have two arrays looking something like the below. I need to write an algorithm that can search all the occurrences of array1 in array2 and taking properties in the objects into account. In this example, it needs to take both source, target, and its respective labels into account. I further need to save the occurrences I find in a separate array like the example at the bottom.

I hope it makes sense. I have been struggling to find a solution that scales when the arrays grow. Thus, where I do not hard code my individual checks.

I need to know some data inside the individual occurrences in arra2, which is why I need to save the occurrences.

     const array1 = [
      {
        id: '11',
        data: { label: 'label2' },
      },
      {
        id: '12',
        data: { label: 'label1' },
      },
      [
        {
          id: '1',
          source: '11',
          target: '12',
          label: 'relation1',
        },
      ],
    ];
    const array2 = [
      {
        id: '279',
        data: {
          label: 'label1',
          unitNumber: '4008840832',
          backgroundColor: 'rgba(255, 255, 255, 1)',
          borderColor: 'rgba(0, 0, 0, 1)',
        },
      },
      {
        id: '280',
        data: {
          label: 'label2',
          unitNumber: '4008840831',
          backgroundColor: 'rgba(255, 255, 255, 1)',
          borderColor: 'rgba(0, 0, 0, 1)',
        },
      },
      {
        id: '281',
        type: 'custom',
        data: {
          label: 'label1',
          unitNumber: '4008862861',
          backgroundColor: 'rgba(255, 255, 255, 1)',
          borderColor: 'rgba(0, 0, 0, 1)',
        },
      },
      {
        id: '282',
        type: 'custom',
        data: {
          label: 'label2',
          unitNumber: null,
          backgroundColor: 'rgba(255, 255, 255, 1)',
          borderColor: 'rgba(0, 0, 0, 1)',
        },
      },
      {
        id: '273',
        source: '280',
        target: '279',
        style: { stroke: 'rgba(0, 0, 0, 1)' },
        data: {
          label: 'relation1',
        },
      },
      {
        id: '274',
        source: '279',
        target: '281',
        style: { stroke: 'rgba(0, 0, 0, 1)' },
        data: {
          label: 'relation2',
        },
      },
      {
        id: '280',
        source: '282',
        target: '279',
        style: { stroke: 'rgba(0, 0, 0, 1)' },
        data: {
          label: 'relation1',
        },
      },
    ];
    const expectedResult = [[
      {
        id: '280',
        data: {
          label: 'label2',
          displayName: 'whatever',
          unitNumber: '4008840831',
          backgroundColor: 'rgba(255, 255, 255, 1)',
          borderColor: 'rgba(0, 0, 0, 1)',
        },
      },
      {
        id: '279',
        data: {
          label: 'label1',
          displayName: 'test',
          unitNumber: '4008840832',
          backgroundColor: 'rgba(255, 255, 255, 1)',
          borderColor: 'rgba(0, 0, 0, 1)',
        },
      },
      {
        id: '273',
        source: '280',
        target: '279',
        style: { stroke: 'rgba(0, 0, 0, 1)' },
        data: {
          label: 'relation1',
        },
      },
    ], [
      {
        id: '282',
        type: 'custom',
        data: {
          label: 'label2',
          displayName: 'random',
          unitNumber: null,
          backgroundColor: 'rgba(255, 255, 255, 1)',
          borderColor: 'rgba(0, 0, 0, 1)',
        },
      },
      {
        id: '279',
        data: {
          label: 'label1',
          displayName: 'test',
          unitNumber: '4008840832',
          backgroundColor: 'rgba(255, 255, 255, 1)',
          borderColor: 'rgba(0, 0, 0, 1)',
        },
      },
      {
        id: '280',
        source: '282',
        target: '279',
        style: { stroke: 'rgba(0, 0, 0, 1)' },
        data: {
          label: 'relation1',
        },
      },
    ]];

Below is a visual representation of the problem.

Flow 1 Flow 2 Result
condition workspace result
example2 flowflow flow22
  • Does this answer your question? [How to get the difference between two arrays of objects in JavaScript](https://stackoverflow.com/questions/21987909/how-to-get-the-difference-between-two-arrays-of-objects-in-javascript) – A. Meshu Jul 27 '21 at 11:30
  • Unfortunatly not. This give me the difference between two arrays. I need to know every single occurrence. Which may exist more than once. By filtering non relevant data from the array. I am still left with an array I do not know how to split into single matching occurrences of array1. – keanottesen Jul 27 '21 at 11:33
  • Why is the expected output a nested array? – trincot Jul 27 '21 at 11:36
  • becouse it is two seperated results. If I match the two arrays it finds two separated results that matches with its labels, sources and targets – keanottesen Jul 27 '21 at 11:37
  • For a visual representation refer to this https://github.com/wbkd/react-flow/issues/1382 – keanottesen Jul 27 '21 at 11:37

0 Answers0