Array concating based on sequence of object Ids in between specific objects

I have two different arrays and want to add the array objects in the final array from the second one if objects are available in between object ids.

firstArray = [
    { id: 'LOA13', type: 'LOAD', otherProperty: 11 },
    { id: 'UNA1', type: 'UNLOAD', otherProperty: 22 },
    { id: 'LOA14', type: 'LOAD', otherProperty: 33 },
    { id: 'UNA15', type: 'UNLOAD', otherProperty: 44 }
]

secondArray = [
    { id: 'LOA13', type: 'LOAD', otherProperty: 12 },
    { id: '123', type: 'DRAG', otherProperty: 34 }, 
    { id: 'UNA1', type: 'UNLOAD', otherProperty: 56 },
    { id: 'LOA14', type: 'LOAD', otherProperty: 12 },
    { id: '456', type: 'DRAG', otherProperty: 1212 },
    { id: '789', type: 'DRAG', otherProperty: 9898 },   
    { id: 'UNA15', type: 'UNLOAD', otherProperty: 56 }
]

If any objects are found between specific Ids sequences then They should add to the result in the array.

The expected result is like:

[
    { id: 'LOA13', type: 'LOAD', otherProperty: 11 }, 
    { id: '123', type: 'DRAG', otherProperty: 34 }, // ADDED as found between ID= LOA13 and UNA1
    { id: 'UNA1', type: 'UNLOAD', otherProperty: 22 }, 
    { id: 'LOA14', type: 'LOAD', otherProperty: 33 },
    { id: '456', type: 'DRAG', otherProperty: 1212 }, // ADDED as found between ID=LOA14 and UNA15
    { id: '789', type: 'DRAG', otherProperty: 9898 }, // ADDED as found between ID=LOA14 and UNA15  
    { id: 'UNA15', type: 'UNLOAD', otherProperty: 44 }
]

Answer

Please use this code.

const resultArray = secondArray.map((val, index) => {
    if(firstArray.filter(value => value.id == val.id).length == 0) {
        let pos = index - 1;
        while(firstArray.filter(value => value.id == secondArray[pos].id).length == 0) pos--;
        const betweenIndex = firstArray.indexOf(firstArray.filter(value => value.id == secondArray[pos].id)[0]);
        const result = "ADDED as found between ID= " + firstArray[betweenIndex].id + " and " + firstArray[betweenIndex+1].id;
        return {...val, result};
    }
    return val;
});
console.log(resultArray);