[英]JavaScript - sort 2 object arrays on the same field
在 JavaScript 中,我有 2 个对象数组,它们具有相同的对象但顺序不同。 我试图弄清楚如何根据另一个数组的顺序对一个数组进行排序。 他们共享一个独特的字段(下面的 sortField )我只是无法弄清楚如何使用它进行排序。 这是我的数组的一个例子:
排序器数组:
[
{
"displayName": "Party",
"sortField": "com.uniqueXbd",
"elementId": "PtyListPanel"
}, {
"displayName": "Group",
"sortField": "com.uniqueARd",
"elementId": "GrpListPaneARd"
}, {
"displayName": "Leader",
"sortField": "com.uniqueEcF",
"elementId": "LeaderListPaneEcF"
}
]
需要排序的数组:
[
{
"displayName": "Group",
"sortField": "com.uniqueARd",
"elementId": "GrpListPaneARd"
}, {
"displayName": "Leader",
"sortField": "com.uniqueEcF",
"elementId": "LeaderListPanel"
}, {
"displayName": "Party",
"sortField": "com.uniqueXbd",
"elementId": "PtyListPaneEcF"
}
]
我猜它看起来会像这样?
needsSorted.sort((a, b) => {
if(sorter.sortField...){
return 1
})
谢谢
const output = [];
sortedArray.forEach( sortedItem => {
const matchingItem = unsortedArray.find( unsortedItem => unsortedItem.sortField === sortedItem.sortField );
if(matchingItem){
output.push(matchingItem);
}
});
由于您知道第二个数组是您希望第一个数组中的项目所在的顺序,因此您应该循环遍历它。 然后从第一个列表中找到匹配的项目,并按该顺序将其推送到您的输出中。
您可以进行排序查找,将排序键映射到原始数组中的索引。 然后在您的排序中,您可以在比较中查找两个对象。
这用恒定时间对象查找代替了每次比较时在原始数组中查找索引的重复需要,因此对于更大的数组,它应该具有更高的性能,但会牺牲查找对象的空间。
let sortObj = [{"displayName": "Party","sortField": "com.uniqueXbd","elementId": "PtyListPanel"}, {"displayName": "Group","sortField": "com.uniqueARd","elementId": "GrpListPaneARd"}, {"displayName": "Leader","sortField": "com.uniqueEcF","elementId": "LeaderListPaneEcF"}] let needsSorted = [{"displayName": "Group","sortField": "com.uniqueARd","elementId": "GrpListPaneARd"}, {"displayName": "Leader","sortField": "com.uniqueEcF","elementId": "LeaderListPanel"}, {"displayName": "Party","sortField": "com.uniqueXbd","elementId": "PtyListPaneEcF"}] let sortLookup = sortObj.reduce((obj, item, idx) => { obj[item.sortField] = idx return obj }, {}) needsSorted.sort((a,b) => sortLookup[a.sortField] - sortLookup[b.sortField]) console.log(needsSorted)
var obj = [
{
"one": 1,
"two": 9
}, {
"one": 3,
"two": 5
}, {
"one": 1,
"two": 2
}
];
var obj = [
{
"one": 1,
"two": 2,
}, {
"one": 1,
"two": 9
}, {
"one": 3,
"two": 5
}
];
obj.sort(function(a, b) {
return a["one"] - b["one"] || a["two"] - b["two"];
});
const sortedIndexes = sorter.map(i => i.sortField);
needsSorted.sort((a, b) => {
const aIndex = sortedIndexes.findIndex((i) => i === a.sortField);
const bIndex = sortedIndexes.findIndex((i) => i === b.sortField);
return aIndex - bIndex;
})
鉴于您只想比较两个数组并确保它们仍然相同,我会以不同的方式进行:
const first = sorted.sort((a, b) => a.localCompare(b))
const second = needsSorting.sort((a, b) => a.localCompare(b))
if (JSON.stringify(first) != JSON.stringify(second)) {
console.log("the array was modified!");
}
const sortOrder = sorted.map(item => item.sortField);
needsSorted.sort((a, b) => {
return sortOrder.indexOf(a.sortField) > sortOrder.indexOf(b.sortField) ? 1 : -1;
});
const fields = sorted.map(x => x.sortField);
const value = x => fields.indexOf(x.sortField);
needSorted.sort((a, b) => value(a) - value(b));
console.log(needSorted);
const sorted = [ { displayName: "Party", sortField: "com.uniqueXbd", elementId: "PtyListPanel" }, { displayName: "Group", sortField: "com.uniqueARd", elementId: "GrpListPaneARd" }, { displayName: "Leader", sortField: "com.uniqueEcF", elementId: "LeaderListPaneEcF" } ]; const needSorted = [ { displayName: "Group", sortField: "com.uniqueARd", elementId: "GrpListPaneARd" }, { displayName: "Leader", sortField: "com.uniqueEcF", elementId: "LeaderListPanel" }, { displayName: "Party", sortField: "com.uniqueXbd", elementId: "PtyListPaneEcF" } ]; const fields = sorted.map(x => x.sortField); const value = x => fields.indexOf(x.sortField); needSorted.sort((a, b) => value(a) - value(b)); console.log(needSorted);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.