Typescript - 如何根据具有已排序对象的字段的另一个数组对一个对象数组进行排序?
例如:
数组A未排序
对象的字段已排序的数组B
尝试根据Array B的顺序对Array A进行排序。
const unsortedArray = [
{
repId: "4",
symbol: "MSLA",
orderNo: "20180518-00004"
},
{
repId: "2",
symbol: "TSLA",
orderNo: "20180518-00003"
},
{
repId: "55",
symbol: "APPL",
orderNo: "20180518-00001"
},
{
repId: "22",
symbol: "FB",
orderNo: "20180518-0002"
}]
const sortedArrayField = [
"20180518-00001",
"20180518-00002",
"20180518-00003",
"20180518-00004"
]
// This is the sorted order that I want
const sortedArray = [
{
repId: "55",
symbol: "APPL",
orderNo: "20180518-00001"
},
{
repId: "22",
symbol: "FB",
orderNo: "20180518-00002"
},
{
repId: "2",
symbol: "TSLA",
orderNo: "20180518-00003"
},
{
repId: "4",
symbol: "MSLA",
orderNo: "20180518-00004"
}]
排序的字段数组可以是任何东西,不仅仅是订单号,可以是符号,状态等......任何建议???
查看了一些示例并尝试了一些方法,但到目前为止还没有优雅的解决方案。没有外部库plz。
答案 0 :(得分:2)
您可以使用排序数组中orderNo的索引对数组进行排序:
const unsortedArray = [
{
repId: "4",
symbol: "MSLA",
orderNo: "20180518-00004"
},
{
repId: "2",
symbol: "TSLA",
orderNo: "20180518-00003"
},
{
repId: "55",
symbol: "APPL",
orderNo: "20180518-00001"
},
{
repId: "22",
symbol: "FB",
orderNo: "20180518-00002"
}]
const sortedArrayField = [
"20180518-00001",
"20180518-00002",
"20180518-00003",
"20180518-00004"
];
// Sort the array by index of orderNo in the sortedArray
unsortedArray.sort((x, y) =>
sortedArrayField.indexOf(x.orderNo) - sortedArrayField.indexOf(y.orderNo)
);
console.log(unsortedArray);

答案 1 :(得分:1)
如果您知道该字段需要根据它进行排序,那么@yadejo解决方案是正确的。
如果不这样做,请尝试以下解决方案
const unsortedArray = [
{
repId: '4',
symbol: 'MSLA',
orderNo: '20180518-00004'
},
{
repId: '2',
symbol: 'TSLA',
orderNo: '20180518-00003'
},
{
repId: '55',
symbol: 'APPL',
orderNo: '20180518-00001'
},
{
repId: '22',
symbol: 'FB',
orderNo: '20180518-00002'
}];
const sortedArrayField = [
'20180518-00001',
'20180518-00002',
'20180518-00003',
'20180518-00004'
];
const sortedArrayOfObject = [];
for (const sortedItem of sortedArrayField) {
sortedArrayOfObject.push(unsortedArray.find(item => {
for (const prop in item) {
if (item[prop] === sortedItem) {
return true;
}
}
return false;
}));
}
console.log(sortedArrayOfObject);
答案 2 :(得分:0)
尝试以下方法:
const unsortedArray = [
{
repId: "4",
symbol: "MSLA",
orderNo: "20180518-00004"
},
{
repId: "2",
symbol: "TSLA",
orderNo: "20180518-00003"
},
{
repId: "55",
symbol: "APPL",
orderNo: "20180518-00001"
},
{
repId: "22",
symbol: "FB",
orderNo: "20180518-00002"
}];
const sortedArrayField = [
"20180518-00001",
"20180518-00002",
"20180518-00003",
"20180518-00004"
];
unsortedArray.sort(function(obj1, obj2){
if(sortedArrayField.indexOf(obj1.orderNo) != -1 && sortedArrayField.indexOf(obj2.orderNo) != -1){
return sortedArrayField.indexOf(obj1.orderNo)- sortedArrayField.indexOf(obj2.orderNo);
} else if(sortedArrayField.indexOf(obj1.orderNo) != -1 && sortedArrayField.indexOf(obj2.orderNo) == -1){
return 0;
} else {
return 1;
}
});
console.log(unsortedArray);

答案 3 :(得分:0)
你可以这样做:sortedArrayField.map(orderNo => unsortedArray.find(item => item.orderNo === orderNo))
如果您在同一位置有多个对象,也可以使用此选项:
var sortedArray2 = [];
sortedArrayField.forEach(orderNo => {
sortedArray2 = sortedArray2.concat(unsortedArray.filter(item => item.orderNo === orderNo));
});
const unsortedArray = [
{
repId: "4",
symbol: "MSLA",
orderNo: "20180518-00004"
},
{
repId: "2",
symbol: "TSLA",
orderNo: "20180518-00003"
},
{
repId: "55",
symbol: "APPL",
orderNo: "20180518-00001"
},
{
repId: "22",
symbol: "FB",
orderNo: "20180518-00002"
},
{
repId: "22 (2)",
symbol: "FB",
orderNo: "20180518-00002"
}]
const sortedArrayField = [
"20180518-00001",
"20180518-00002",
"20180518-00003",
"20180518-00004"
];
var sortedArray = sortedArrayField.map(orderNo => unsortedArray.find(item => item.orderNo === orderNo));
console.log(sortedArray);
var sortedArray2 = [];
sortedArrayField.forEach(orderNo => {
sortedArray2 = sortedArray2.concat(unsortedArray.filter(item => item.orderNo === orderNo));
});
console.log(sortedArray2);