javascriptangulartypescriptangular8

How to search data inside two different array and copy the matching data into new array in Angular 8


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"                  
                }
            ]
        ]
    }
];

Solution

  • 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);