我一直在努力根據該陣列中不存在的屬性對物件陣列進行排序。我有我的第一個陣列
const scores = [
{
"scoreDefinitionName": "_mo45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573564",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "_sc45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573568",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "_ua45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573572",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "KFT",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573576",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "LFT",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573580",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "dummy",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573584",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "Score",
"score": 0.32,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573588",
"globalId": null,
"version": 0
}
}
]
第二個陣列就像
[
{
variableName: "KFT",
variable: "KFT",
sequence: 1,
variableDetail: {
displayName: ""
},
},
{
variableName: "LFT",
variable: "LFT",
sequence: 3,
variableDetail: {
displayName: ""
},
},
{
variableName: "Dummy",
variable: "dummy",
sequence: 2,
variableDetail: {
displayName: ""
}
}
];
所以基本上我想要做的是scores根據該物件的順序對第一個陣列進行排序,該物件在varibles名稱屬性匹配時與第二個陣列中的物件匹配。
我發現的一種解決方法是首先將陣列的屬性推送到陣列中sequence,然后根據該屬性進行排序,但我相信它可以進行更多優化。還有其他方法可以使其更優化嗎?variablesscores
uj5u.com熱心網友回復:
下面介紹的是實作所需目標的一種可能方法,假設scores陣列需要根據陣列上的 prop 進行排序(sequence而variables不是variables陣列的填充順序)。也就是說,想要的排序順序是KFT、Dummy、LFT,分別為sequence1、2、3;而不是KFT、LFT、Dummy對應sequence的是1、3、2。
代碼片段
// method to sort using custom logic (without mutating the input array)
const mySort = (arr, ord) => (
structuredClone(arr) // deep-clone to avoid mutating parameter array
.sort(
(
{scoreDefinitionName: sdA}, // de-structure to access "scoreDefinitionName"
{scoreDefinitionName: sdB} // of both 'a' and 'b'
) => {
if (sdA in ord && sdB in ord) { // if both "scoreDefinitionName"s are in variables
return ord[sdA] - ord[sdB] // use the "sequence number to sort
} else if (sdA in ord) { // else, if only 'a' is in 'variables' array
return -1;
} else if (sdB in ord) { // 'a' is not but 'b' is in 'variables' array
return 1;
} else return ( // both 'a' and 'b' are not in the array
sdA < sdB ? -1 : 1 // sort using the "scoreDefinitionName"
);
}
) // implicit return of the "sort"-ed array
);
const scores = [
{
"scoreDefinitionName": "_mo45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573564",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "_sc45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573568",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "_ua45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573572",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "KFT",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573576",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "LFT",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573580",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "dummy",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573584",
"globalId": null,
"version": 0
}
},
{
"scoreDefinitionName": "Score",
"score": 0.32,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573588",
"globalId": null,
"version": 0
}
}
];
const variables = [
{
variableName: "KFT",
variable: "KFT",
sequence: 1,
variableDetail: {
displayName: ""
},
},
{
variableName: "LFT",
variable: "LFT",
sequence: 3,
variableDetail: {
displayName: ""
},
},
{
variableName: "Dummy",
variable: "dummy",
sequence: 2,
variableDetail: {
displayName: ""
}
}
];
// invoke the custom sort method
// transform 'variables' array into an object
// with key-value pair as 'variable': 'sequence'
// This helps in sorting using the sequence
console.log(
'sorting scores arr using ordering from variables array...\n',
mySort(
scores,
Object.fromEntries(
variables.map(
({ variable, sequence }) => [variable, sequence]
)
)
)
);
.as-console-wrapper { max-height: 100% !important; top: 0 }
解釋
添加到上述代碼段的行內注釋。
uj5u.com熱心網友回復:
比較函式可以通過以下方式實作:它首先根據預定義優先級條目的系結映射比較分數項,其中key從variable值中檢索到優先級,然后從第二個陣列項value的值中檢索優先級step根據升序sequence比較 score-items 的s。scoreDefinitionName
function compareScoreItemsByDefinitionNameAndBoundPrecedence(a, b) {
const precedenceLookup = this;
const aName = a.scoreDefinitionName;
const bName = b.scoreDefinitionName;
const aPrecedence = precedenceLookup.get(aName) ?? null;
const bPrecedence = precedenceLookup.get(bName) ?? null;
return (
(aPrecedence === null && bPrecedence !== null && 1) ||
(aPrecedence !== null && bPrecedence === null && -1) ||
(aPrecedence !== null && bPrecedence !== null && aPrecedence - bPrecedence) ||
// ascending name order if no predefined precedence
// is available or precedence comparison value was zero.
aName.localeCompare(bName)
);
}
const namePrecedence = [{
variableName: "KFT",
variable: "KFT",
sequence: 1,
variableDetail: {
displayName: ""
},
}, {
variableName: "LFT",
variable: "LFT",
sequence: 3,
variableDetail: {
displayName: ""
},
}, {
variableName: "Dummy",
variable: "dummy",
sequence: 2,
variableDetail: {
displayName: ""
}
}];
const scores = [{
"scoreDefinitionName": "_mo45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573564",
"globalId": null,
"version": 0
}
}, {
"scoreDefinitionName": "_sc45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573568",
"globalId": null,
"version": 0
}
}, {
"scoreDefinitionName": "_ua45",
"score": 0,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573572",
"globalId": null,
"version": 0
}
}, {
"scoreDefinitionName": "KFT",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573576",
"globalId": null,
"version": 0
}
}, {
"scoreDefinitionName": "LFT",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573580",
"globalId": null,
"version": 0
}
}, {
"scoreDefinitionName": "dummy",
"score": 1,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573584",
"globalId": null,
"version": 0
}
}, {
"scoreDefinitionName": "Score",
"score": 0.32,
"scoreDefinitionId": {
"objectType": "ScoreDefinition",
"idValue": "573588",
"globalId": null,
"version": 0
}
}];
console.log(
'... key value tuple of the to be created and bound precedence map ...',
namePrecedence
.map(({ variable, sequence }) => [variable, sequence])
);
scores.sort(
compareScoreItemsByDefinitionNameAndBoundPrecedence
.bind(
new Map(
namePrecedence
.map(({ variable, sequence }) => [variable, sequence])
)
)
);
console.log({ scores });
.as-console-wrapper { min-height: 100%!important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/482232.html
標籤:javascript 数组 排序 相比 抬头
