skip to Main Content

I’ve the following array:

const arr1 = [
 {"id": 699306, "address": "On earth" },
 {"id": 701298, "address": "On earth" },
 {"id": 701299, "address": "On earth" },
 {"id": 701300, "address": "On earth" }
];

Another array is pretty complex as follows:

const arr2 = [
{
    "included": [
        {
            "id": 699306,
            "levelColumn": "CLAIM_DATE",
            "levelColumnName": "Claims",
            "isIncluded": true,
            "tabs": [
                {
                    "tabId": 1231,
                    "tabName": "Files"
                }
            ],
            "levels": [
                {
                    "permValue": "01/01/2021",
                    "permName": "01/01/2021"
                },
                {
                    "permValue": "01/01/2022",
                    "permName": "01/01/2022"
                },
                {
                    "permValue": "01/01/2023",
                    "permName": "01/01/2023"
                },
                {
                    "permValue": "01/01/2024",
                    "permName": "01/01/2024"
                },
                {
                    "permValue": "01/01/2025",
                    "permName": "01/01/2025"
                },
                {
                    "permValue": "01/02/2023",
                    "permName": "01/02/2023"
                },
                {
                    "permValue": "01/02/2024",
                    "permName": "01/02/2024"
                },
                {
                    "permValue": "01/03/2024",
                    "permName": "01/03/2024"
                },
                {
                    "permValue": "01/03/2026",
                    "permName": "01/03/2026"
                },
                {
                    "permValue": "01/04/2022",
                    "permName": "01/04/2022"
                },
                {
                    "permValue": "01/04/2023",
                    "permName": "01/04/2023"
                },
                {
                    "permValue": "01/04/2024",
                    "permName": "01/04/2024"
                }
            ]
        },
        {
            "id": 701298,
            "levelColumn": "CLAIM_DATE",
            "levelColumnName": "Claims",
            "isIncluded": true,
            "tabs": [
                {
                    "tabId": 1231,
                    "tabName": "Files"
                }
            ],
            "levels": [
                {
                    "permValue": "01/01/2021",
                    "permName": "01/01/2021"
                },
                {
                    "permValue": "01/01/2022",
                    "permName": "01/01/2022"
                }
            ]
        },
        {
            "id": 647298,
            "levelColumn": "SOURCE_FILE_ID",
            "levelColumnName": "Files",
            "isIncluded": true,
            "tabs": [
                {
                    "tabId": 1231,
                    "tabName": "Files"
                },
                {
                    "tabId": 1251,
                    "tabName": "Files - noinv"
                }
            ],
            "levels": [
                {
                    "permValue": "2028524",
                    "permName": "0000841354-LAMB-0303043272-11_17_2023_A_Gold.txt"
                },
                {
                    "permValue": "2092815",
                    "permName": "0000841354-LAMB-0305232305-051324_A_Gold.txt"
                },
                {
                    "permValue": "2093469",
                    "permName": "0000841354-LAMB-1135418182-051624_A_Gold.txt"
                },
                {
                    "permValue": "2125382",
                    "permName": "0000841354-LAMB-303368862-12_12_23_3020_Gold.txt"
                }
            ]
        },
        {
            "id": 699307,
            "levelColumn": "CLAIM_DATE",
            "levelColumnName": "Claims",
            "isIncluded": true,
            "tabs": [
                {
                    "tabId": 1231,
                    "tabName": "Files"
                }
            ],
            "levels": [
                {
                    "permValue": "01/01/2021",
                    "permName": "01/01/2021"
                },
                {
                    "permValue": "01/01/2022",
                    "permName": "01/01/2022"
                },
                {
                    "permValue": "01/01/2023",
                    "permName": "01/01/2023"
                },
                {
                    "permValue": "01/01/2024",
                    "permName": "01/01/2024"
                },
                {
                    "permValue": "01/01/2025",
                    "permName": "01/01/2025"
                },
                {
                    "permValue": "01/02/2023",
                    "permName": "01/02/2023"
                },
                {
                    "permValue": "01/02/2024",
                    "permName": "01/02/2024"
                },
                {
                    "permValue": "01/03/2024",
                    "permName": "01/03/2024"
                },
                {
                    "permValue": "01/03/2026",
                    "permName": "01/03/2026"
                },
                {
                    "permValue": "01/04/2022",
                    "permName": "01/04/2022"
                },
                {
                    "permValue": "01/04/2023",
                    "permName": "01/04/2023"
                },
                {
                    "permValue": "01/04/2024",
                    "permName": "01/04/2024"
                }
            ]
        },
        {
            "id": 701298,
            "levelColumn": "MSTR_ENTERPRISE_ID",
            "levelColumnName": "Enterprise",
            "isIncluded": true,
            "tabs": [
                {
                    "tabId": 1971,
                    "tabName": "Users"
                }
            ],
            "levels": [
                {
                    "permValue": "23",
                    "permName": "Aramark"
                },
                {
                    "permValue": "693",
                    "permName": "Aryzta"
                }
            ]
        },
        {
            "id": 693298,
            "levelColumn": "E1_APP_USER_ID",
            "levelColumnName": "User",
            "isIncluded": true,
            "tabs": [
                {
                    "tabId": 1971,
                    "tabName": "Users"
                }
            ],
            "levels": [
                {
                    "permValue": "137064",
                    "permName": "   (tibersoftsmuckers [137064])"
                },
                {
                    "permValue": "136184",
                    "permName": " QA (tsoceanspray [136184])"
                }
            ]
        },
        {
            "id": 693298,
            "levelColumn": "E1_APP_USER_TYPE",
            "levelColumnName": "User Type",
            "isIncluded": true,
            "tabs": [
                {
                    "tabId": 1971,
                    "tabName": "Users"
                }
            ],
            "levels": [
                {
                    "permValue": "Client",
                    "permName": "Client"
                },
                {
                    "permValue": "Not Provided",
                    "permName": "Not Provided"
                },
                {
                    "permValue": "Tibersoft",
                    "permName": "Tibersoft"
                }
            ]
        }
    ],
    "excluded": [
        {
            "id": 368302,
            "levelColumn": "E1_APP_USER_ID",
            "levelColumnName": "User",
            "isIncluded": false,
            "tabs": [
                {
                    "tabId": 1971,
                    "tabName": "Users"
                }
            ],
            "levels": [
                {
                    "permValue": "575301",
                    "permName": " Kelley Mahowald (kmahowald [575301])"
                },
                {
                    "permValue": "134560",
                    "permName": " Support (support [134560])"
                },
                {
                    "permValue": "135241",
                    "permName": " Support (support [135241])"
                },
                {
                    "permValue": "138248",
                    "permName": " Support (support [138248])"
                },
                {
                    "permValue": "136725",
                    "permName": " Tibersoftaramark (tibersoftaramark [136725])"
                },
                {
                    "permValue": "137705",
                    "permName": " Tibersoftjmfg (tibersoftjmfg [137705])"
                },
                {
                    "permValue": "138508",
                    "permName": " Tibersoftlw (tibersoftlw [138508])"
                },
                {
                    "permValue": "139330",
                    "permName": " Tibersoftschwans (tibersoftschwans [139330])"
                },
                {
                    "permValue": "136726",
                    "permName": " Tsconagra (tsaramark [136726])"
                },
                {
                    "permValue": "134466",
                    "permName": "(abailey [134466])"
                },
                {
                    "permValue": "134284",
                    "permName": "(afitzgerald [134284])"
                },
                {
                    "permValue": "137484",
                    "permName": "(c [137484])"
                },
                {
                    "permValue": "31346",
                    "permName": "(caradp [31346])"
                },
                {
                    "permValue": "31658",
                    "permName": "(caramarie [31658])"
                }
            ]
        }
    ]
  }
]

So what I want is to merge the two arrays into one as follows:

const arr1 = [
 {"id": 699306, "address": "On earth", "included": [items from arr2], "excluded": [items from arr2] },
 {"id": 701298, "address": "On earth", "included": [items from arr2], "excluded": [items from arr2] },
 {"id": 701299, "address": "On earth", "included": [], "excluded": [] }, //As there is no matching for these ids
 {"id": 701300, "address": "On earth", "included": [], "excluded": [] }
];

I know, the relationship doesn’t look appropriate but this is what I’ve now. Any way to map them?

I tried the following but it returns null for both include and exclude object:

mapArraysById(array1: Item[], array2: Item[]): Item[] {
  const mergedArray: Item[] = [];

  for (let i = 0; i < array1.length; i++) {
    const item1 = array1[i];
    console.log("id: " + item1.id);

    const item2 = array2.find(item => item.id === item1.id);

    if (item2) {
      console.log("Item 2:");
      console.log(item1 + " " + item2);
      mergedArray.push({ ...item1, ...item2 });
    } else {
      mergedArray.push({ ...item1, included: [], excluded: [] });
    }
  }

  return mergedArray;
}

2

Answers


  1. Try iterating through arr1 and get included and excluded items in arr2 that match the id. Finally, returns included and excluded items to result and neither of them matches returns an empty array.

    Refer the below code reference:

    const arr1 = [
      { id: 699306, address: "On earth" },
      { id: 701298, address: "On earth" },
      { id: 701299, address: "On earth" },
      { id: 701300, address: "On earth" },
    ];
    
    const arr2 = [
      {
        included: [
          {
            id: 699306,
            levelColumn: "CLAIM_DATE",
            levelColumnName: "Claims",
            isIncluded: true,
            tabs: [{ tabId: 1231, tabName: "Files" }],
            levels: [
              { permValue: "01/01/2021", permName: "01/01/2021" },
              { permValue: "01/01/2022", permName: "01/01/2022" },
            ],
          },
          {
            id: 701298,
            levelColumn: "CLAIM_DATE",
            levelColumnName: "Claims",
            isIncluded: true,
            tabs: [{ tabId: 1231, tabName: "Files" }],
            levels: [
              { permValue: "01/01/2021", permName: "01/01/2021" },
              { permValue: "01/01/2022", permName: "01/01/2022" },
            ],
          },
        ],
        excluded: [
          {
            id: 368302,
            levelColumn: "E1_APP_USER_ID",
            levelColumnName: "User",
            isIncluded: false,
            tabs: [{ tabId: 1971, tabName: "Users" }],
            levels: [
              { permValue: "575301", permName: " Kelley Mahowald (kmahowald [575301])" },
            ],
          },
        ],
      },
    ];
    
    const mergeArrays = (arr1, arr2) => {
      const { included, excluded } = arr2[0]; // get included and excluded from arr2
      return arr1.map(item => {
        const includedItems = included.filter(i => i.id === item.id); // get matching included items
        const excludedItems = excluded.filter(e => e.id === item.id); // get matching excluded items
    
        return {
          ...item,
          included: includedItems.length > 0 ? includedItems : [],
          excluded: excludedItems.length > 0 ? excludedItems : [],
        };
      });
    };
    
    console.log(JSON.stringify(mergeArrays(arr1, arr2)));
    Login or Signup to reply.
  2. const arr1 = [
      { "id": 699306, "address": "On earth" },
      { "id": 701298, "address": "On earth" },
      { "id": 701299, "address": "On earth" },
      { "id": 701300, "address": "On earth" }
    ];
    
    const arr2 = [
      {
        "included": [
          {
            "id": 699306,
            "levelColumn": "CLAIM_DATE",
            "levelColumnName": "Claims",
            "isIncluded": true
          },
          {
            "id": 701298,
            "levelColumn": "CLAIM_DATE",
            "levelColumnName": "Claims",
            "isIncluded": true
          }
        ],
        "excluded": [
          {
            "id": 699306,
            "levelColumn": "CLAIM_DATE",
            "levelColumnName": "Claims",
            "isIncluded": false
          }
        ]
      }
    ];
    
    // Step 1: Index the `included` and `excluded` arrays for quick lookup
    const includedIndex = arr2[0].included.reduce((map, item) => {
      if (!map[item.id]) map[item.id] = [];
      map[item.id].push(item);
      return map;
    }, {});
    
    const excludedIndex = arr2[0].excluded.reduce((map, item) => {
      if (!map[item.id]) map[item.id] = [];
      map[item.id].push(item);
      return map;
    }, {});
    
    // Step 2: Map over `arr1` to add `included` and `excluded` data
    const result = arr1.map(item => ({
      ...item,
      included: includedIndex[item.id] || [],
      excluded: excludedIndex[item.id] || []
    }));
    
    console.log(result);
    

    Explanation:
    Indexing for Efficiency:

    The reduce() method creates lookup objects (includedIndex and excludedIndex) where each id is the key and the value is an array of matching items.
    This ensures constant time complexity (O(1)) for lookups during the merge.
    Mapping Over arr1:

    For each item in arr1, retrieve its included and excluded data using the pre-built indexes.
    If no matches are found, default to an empty array ([]).

    Final Result:

    The output is a merged array where each item in arr1 contains its corresponding included and excluded data.

    Complexity:
    Indexing: O(n), where n is the size of the included and excluded arrays.
    Mapping: O(m), where m is the size of arr1.
    Total Complexity: O(n + m), which is optimal for this problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search