Order or sort multiple arrays by indexs of matched strings in another array using lodash or JS

I am trying to have my arrays sorted by the indexes of strings in another array.

I am getting back the following:

const matchtoThisArray:

['11006', '10240', '10142', '10309', '10367', '10724', '10741', '10362', '10919', '11115', '10590', '10179', '18510', '10051']

if there is a match in one of my arrays, the output will be in that position.

My default output starts with this below, which is at index 0. My desired output is that it would at the end since "10051" is at the end in the "matchThisArray"

     0: Array(36)
        0: {TMSId: 'EP009285440323', rootId: '21253358', time: '17:00', dur: 'PT00H30M', prgSvcId: '10051', …}
        1: {TMSId: 'EP035579760050', rootId: '21253391', time: '17:30', dur: 'PT00H30M', prgSvcId: '10051', …}
        2: {TMSId: 'EP033168400060', rootId: '21166708', time: '18:00', dur: 'PT01H00M', prgSvcId: '10051', …}

1: Array(24)
0: {TMSId: 'EP014781492754', rootId: '21041927', time: '16:00', dur: 'PT01H00M', prgSvcId: '10142', …}
1: {TMSId: 'EP006062994294', rootId: '21041931', time: '17:00', dur: 'PT01H00M', prgSvcId: '10142', …}
2: {TMSId: 'EP041682710002', rootId: '21418098', time: '18:00', dur: 'PT01H00M', prgSvcId: '10142', …}

Filtering method that does not work:

const criteria = ['11006', '10240', '10142', '10309', '10367', '10724', '10741', '10362', '10919', '11115', '10590', '10179', '18510', '10051']
      
    const filterPrgs = resultSchedules.map((rows) => {
            return rows.map((column) => {
              return column;
            });
          });
          const filtered = filterPrgs.filter((obj) => {
           return criteria.indexOf(obj.prgSvcId) >= 0;
          });

Sample Data:

[
   [
      {
         "TMSId":"EP009285440323",
         "rootId":"21253358",
         "time":"1:00",
         "dur":"PT00H30M",
         "prgSvcId":"10053"
      },
      {
         "TMSId":"EP009285440323",
         "rootId":"21253358",
         "time":"14:00",
         "dur":"PT00H30M",
         "prgSvcId":"10053"
      },
      {
         "TMSId":"EP009285440323",
         "rootId":"21253358",
         "time":"1:00",
         "dur":"PT00H30M",
         "prgSvcId":"10053"
      }
   ],
   [
      {
         "TMSId":"EP035579760050",
         "rootId":"21253391",
         "time":"17:30",
         "dur":"PT00H30M",
         "prgSvcId":"10051"
      },
      {
         "TMSId":"EP035579760050",
         "rootId":"21253391",
         "time":"17:10",
         "dur":"PT00H30M",
         "prgSvcId":"10051"
      },
      {
         "TMSId":"EP035579760050",
         "rootId":"21253391",
         "time":"13:30",
         "dur":"PT00H30M",
         "prgSvcId":"10051"
      }
   ],
   [
      {
         "TMSId":"EP033168400060",
         "rootId":"21166708",
         "time":"18:00",
         "dur":"PT01H00M",
         "prgSvcId":"10052"
      },
      {
         "TMSId":"EP033168400060",
         "rootId":"21166708",
         "time":"18:00",
         "dur":"PT01H00M",
         "prgSvcId":"10052"
      },
      {
         "TMSId":"EP033168400060",
         "rootId":"21166708",
         "time":"18:00",
         "dur":"PT01H00M",
         "prgSvcId":"10052"
      }
   ]
]

Solution 1:

There you go :)

let inputArray = getInput()
let positionOfArray = ['10052', '10051', '10053']

console.log(mapArrayByPositionArray(inputArray, positionOfArray))

function mapArrayByPositionArray (array, positionArray) {
 let inputArrayToObject = array.reduce((acc, elem) => {
  acc[elem[0].prgSvcId] = elem
  return acc
 }, {})
 return positionArray.map((positionIndex) => inputArrayToObject[positionIndex])
}

function getInput () {
 return [
   [
      {
         "TMSId":"EP009285440323",
         "rootId":"21253358",
         "time":"1:00",
         "dur":"PT00H30M",
         "prgSvcId":"10053"
      },
      {
         "TMSId":"EP009285440323",
         "rootId":"21253358",
         "time":"14:00",
         "dur":"PT00H30M",
         "prgSvcId":"10053"
      },
      {
         "TMSId":"EP009285440323",
         "rootId":"21253358",
         "time":"1:00",
         "dur":"PT00H30M",
         "prgSvcId":"10053"
      }
   ],
   [
      {
         "TMSId":"EP035579760050",
         "rootId":"21253391",
         "time":"17:30",
         "dur":"PT00H30M",
         "prgSvcId":"10051"
      },
      {
         "TMSId":"EP035579760050",
         "rootId":"21253391",
         "time":"17:10",
         "dur":"PT00H30M",
         "prgSvcId":"10051"
      },
      {
         "TMSId":"EP035579760050",
         "rootId":"21253391",
         "time":"13:30",
         "dur":"PT00H30M",
         "prgSvcId":"10051"
      }
   ],
   [
      {
         "TMSId":"EP033168400060",
         "rootId":"21166708",
         "time":"18:00",
         "dur":"PT01H00M",
         "prgSvcId":"10052"
      },
      {
         "TMSId":"EP033168400060",
         "rootId":"21166708",
         "time":"18:00",
         "dur":"PT01H00M",
         "prgSvcId":"10052"
      },
      {
         "TMSId":"EP033168400060",
         "rootId":"21166708",
         "time":"18:00",
         "dur":"PT01H00M",
         "prgSvcId":"10052"
      }
   ]
]
}