I have two arrays in javascript

```
array1 = [{id: 4, value:'xyz'}, {id: 2, value:'pqr'}, {id: 5, value:'abc'}]
array2 = [
         [{id: 5, value:'abc'}, {id: 4, value:'xyz'}],
         [{id: 5, value:'abc'}, {id: 2, value:'pqr'}],
         [{id: 4, value:'xyz'}, {id: 2, value:'pqr'}],
         [{id: 2, value:'pqr'}, {id: 5, value:'abc'}]
]

```

array2 is array of array of objects,
all these arrays inside array 2 should be first of all sorted as per the array1 sequence and then object with null should be pushed if that particular id doesn't exist at that particular index.

```
output = [
         [{id: 4, value:'xyz'}, {id: null, value:null}, {id: 5, value:'abc'}],
         [{id: null, value:null}, {id: 2, value:'pqr'}, {id: 5, value:'abc'}],
         [{id: 4, value:'xyz'}, {id: 2, value:'pqr'}, {id: null, value:null}],
         [{id: 4, value:'xyz'}, {id: 2, value:'pqr'}, {id: null, value:null}],
         [{id: null, value:null}, {id: 2, value:'pqr'}, {id: 5, value:'abc'}]
].

```

Here is my code

```
export const mapOrder = (array, order, key) => {
    let arr = [];
    array.sort((a, b) => {
        let A = a[key];
        let B = b[key];
        if (order.indexOf(A) > order.indexOf(B)) {
            return 1;
        }
        return -1;
    });
    if (order.length > 0) {
        for (let i = 0; i < order.length; i++) {
            for (let j = 0; j < array.length; j++) {
                if (order[i] === array[j].UserId) {
                    arr.push(array[j]);
                } else {
                    arr.push({ id: null, value: null})
                }
            }
        }
        array = arr;
    }
    return arr;
};

```