skip to Main Content

I have array1 and array2 in below data.i wanted to iterate all factoryId of array1 and need to check if same factoryid is present in array2.

if factoryId of array1 is matching with array2 factoryId , than i wanted to prepare new result array with array1 object and status field from array2 into new result array.

Expected output is a combination of array1 and array2 status field.

can anyone help me to do this

const array1 = [
    {
        "name": "Africa",
        "filterChildren": [
            [
                {
                    "name": "Agbara - Laundry",                  
                    "factoryId": "R_X001"                  
                },
                {
                    "name": "Agbara - Savoury",
                    "factoryId": "R_X002"
                }               
            ]
        ]
    },
    {
        "name": "X-Ekaterra",
        "filterChildren": [
            [
                {
                    "name": "Agbara - Tea",
                    "factoryId": "R_x903"                   
                }
            ]
        ]
    }
];


const array2 = [
    {
        "FactoryId": "R_X001",
        "Status": "Draft"
    },
    {       
        "FactoryId": "R_x903",
        "Status": "Submitted"
    }
]

Expected Output

Expected Result = [
    {
        "name": "Africa",
        "filterChildren": [
            [
                {
                    "name": "Agbara - Laundry",                  
                    "factoryId": "R_X001",
                    "Status": "Draft"
                },
                {
                    "name": "Agbara - Savoury",
                    "factoryId": "R_X002"
                }               
            ]
        ]
    },
    {
        "name": "X-Ekaterra",
        "filterChildren": [
            [
                {
                    "name": "Agbara - Tea",
                    "factoryId": "R_x903",
                    "Status": "Submitted"                  
                }
            ]
        ]
    }
];

2

Answers


  1. Just run a for loop for the arrays, then use array find method to find the element on the second array and update the property of Status.

    const array1 = [
        {
            "name": "Africa",
            "filterChildren": [
                [
                    {
                        "name": "Agbara - Laundry",                  
                        "factoryId": "R_X001"                  
                    },
                    {
                        "name": "Agbara - Savoury",
                        "factoryId": "R_X002"
                    }               
                ]
            ]
        },
        {
            "name": "X-Ekaterra",
            "filterChildren": [
                [
                    {
                        "name": "Agbara - Tea",
                        "factoryId": "R_x903"                   
                    }
                ]
            ]
        }
    ];
    
    
    const array2 = [
        {
            "FactoryId": "R_X001",
            "Status": "Draft"
        },
        {       
            "FactoryId": "R_x903",
            "Status": "Submitted"
        }
    ]
    
    array1.forEach((item) => {
      item.filterChildren.forEach((data) => {
        if(Array.isArray(data)) {
        data.forEach((qwer) => {
          const foundArr = array2.find((a) => a.FactoryId === qwer.factoryId);
          if(foundArr) {
            qwer.Status = foundArr.Status;
          }
        });
        }
      });
    });
    
    console.log(array1);
    Login or Signup to reply.
  2. You can achieve this by iterating over array1 and array2, and merging the status from array2 into array1 when the factoryId matches.

    Here is a function that can do this functionalitie

    function mergeArrays(arr1, arr2) {
        const statusMap = arr2.reduce((map, obj) => {
            map[obj.FactoryId] = obj.Status;
            return map;
        }, {});
        arr1.forEach(region => {
            region.filterChildren.forEach(childArray => {
                childArray.forEach(factory => {
                    if (statusMap[factory.factoryId]) {
                        factory.Status = statusMap[factory.factoryId];
                    }
                });
            });
        });
    
        return arr1;
    }
    

    and you get the the exact output you want

    const result = mergeArrays(array1, array2);
    console.log(JSON.stringify(result, null, 2));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search