使用 lodash 或 JS 按另一个数组中匹配字符串的索引对多个数组进行排序或排序
Order or sort multiple arrays by indexs of matched strings in another array using lodash or JS
我正在尝试让我的数组按另一个数组中的字符串索引排序。
我得到以下信息:
const matchtoThisArray:
['11006', '10240', '10142', '10309', '10367', '10724', '10741', '10362', '10919', '11115', '10590', '10179', '18510', '10051']
如果我的数组之一有匹配项,输出将在那个位置。
我的默认输出从下面这个开始,它位于索引 0。我想要的输出是它会在最后,因为“10051”在“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', …}
过滤方法无效:
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;
});
示例数据:
[
[
{
"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"
}
]
]
好了:)
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"
}
]
]
}
我正在尝试让我的数组按另一个数组中的字符串索引排序。
我得到以下信息:
const matchtoThisArray:
['11006', '10240', '10142', '10309', '10367', '10724', '10741', '10362', '10919', '11115', '10590', '10179', '18510', '10051']
如果我的数组之一有匹配项,输出将在那个位置。
我的默认输出从下面这个开始,它位于索引 0。我想要的输出是它会在最后,因为“10051”在“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', …}
过滤方法无效:
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;
});
示例数据:
[
[
{
"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"
}
]
]
好了:)
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"
}
]
]
}