为什么下面的javascript数组排序决定了我的两个排序顺序参数是未定义的



这是直接从我的代码和控制台日志中复制的。正如你所看到的,它是一个对象数组。我正在尝试根据我在一个单独的对象中定义的排序顺序对对象进行排序。它对七个元素中的五个进行排序,但似乎武断地决定没有定义排序对象的两个属性。该对象由我的代码字符串化,并在表示它未定义的行之前写入日志。显然,它并没有被定义。那么这里发生了什么?

这个对象定义了我想要使用的排序顺序。

const statSortOrder = {
"4043523819": 0, //impact
"1240592695": 1, //range
"155624089": 2,  //stablility
"943549884": 3,  //handling
"4188031367": 4, //reload speed
"4284893193": 5, //rounds per minute
"3871231066": 6 //magazine
};

这段代码应该按照这个顺序对对象数组进行排序。请注意,我在这里有一些额外的东西来吐出控制台发生的事情的详细结果。我将进一步复制结果日志的内容和数组"orderArray"的内容。

console.log("STAT SORT ORDER: n" + JSON.stringify(statSortOrder));
console.log("UNORDERED ARRAY: n" + JSON.stringify(orderArray));
orderArray.sort((a,b) => {
if(a.hash && statSortOrder[a.hash.toString()] && b.hash && statSortOrder[b.hash.toString()]) {
console.log("statSorOrder[a.hash] = " + statSortOrder[a.hash.toString()] + " : statSortOrder[b.hash] = " + statSortOrder[b.hash.toString()]);
return statSortOrder[a.hash.toString()] - statSortOrder[b.hash.toString()];
} else {
console.log("statSortOrder = " + JSON.stringify(statSortOrder) + "n"
+ "No match because a.hash.toString() =  " + a.hash.toString() + " and statSortOrder[a.hash.toString()] = " + statSortOrder[a.hash.toString()] + "n"
+ " and b.hash.toString() = " + b.hash.toString() + " and statSortOrder[b.hash.toString()] = " + statSortOrder[b.hash.toString()]);
return -1;
}
});
console.log("ORDER ARRAY: n" + JSON.stringify(orderArray));

下面是我在日志中捕获的内容:

2017-10-14T16:31:40.594Z    2628c5d7-b0fd-11e7-8b39-9b4f450e9f47    statSorOrder[a.hash] = 2 : statSortOrder[b.hash] = 3
2017-10-14T16:31:40.594Z    2628c5d7-b0fd-11e7-8b39-9b4f450e9f47    statSorOrder[a.hash] = 3 : statSortOrder[b.hash] = 1
2017-10-14T16:31:40.594Z    2628c5d7-b0fd-11e7-8b39-9b4f450e9f47    statSorOrder[a.hash] = 2 : statSortOrder[b.hash] = 1
2017-10-14T16:31:40.594Z    2628c5d7-b0fd-11e7-8b39-9b4f450e9f47    statSorOrder[a.hash] = 3 : statSortOrder[b.hash] = 6
2017-10-14T16:31:40.594Z    2628c5d7-b0fd-11e7-8b39-9b4f450e9f47    
statSortOrder =
{
"155624089": 2,
"943549884": 3,
"1240592695": 1,
"3871231066": 6,
"4043523819": 0,
"4188031367": 4,
"4284893193": 5
}
No match because a.hash.toString() = 3871231066 and 
statSortOrder[a.hash.toString()] = undefined
and b.hash.toString() = 4043523819 and statSortOrder[b.hash.toString()] = 0
2017-10-14T16:31:40.594Z    2628c5d7-b0fd-11e7-8b39-9b4f450e9f47    
statSortOrder =
{
"155624089": 2,
"943549884": 3,
"1240592695": 1,
"3871231066": 6,
"4043523819": 0,
"4188031367": 4,
"4284893193": 5
}
No match because a.hash.toString() = 4043523819 and 
statSortOrder[a.hash.toString()] = undefined
and b.hash.toString() = 4188031367 and statSortOrder[b.hash.toString()] = 4
2017-10-14T16:31:40.594Z    2628c5d7-b0fd-11e7-8b39-9b4f450e9f47    statSorOrder[a.hash] = 4 : statSortOrder[b.hash] = 5

这是在这个排序操作之前和之后对象数组的内容:

之前:

[
{
"entityType": "DestinyStatDefinition",
"index": 19,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Stability",
"ciName": "stability",
"hash": 155624089,
"displayProperties": {
"name": "Stability",
"icon": "/img/misc/missing_icon_d2.png",
"description": "How much or little recoil you will experience while firing the weapon.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "How much or little recoil you will experience while firing the weapon.",
"id": 155624089
}
,
{
"entityType": "DestinyStatDefinition",
"index": 23,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Handling",
"ciName": "handling",
"hash": 943549884,
"displayProperties": {
"name": "Handling",
"icon": "/img/misc/missing_icon_d2.png",
"description": "The speed with which the weapon can be readied and aimed.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "The speed with which the weapon can be readied and aimed.",
"id": 943549884
}
,
{
"entityType": "DestinyStatDefinition",
"index": 15,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Range",
"ciName": "range",
"hash": 1240592695,
"displayProperties": {
"name": "Range",
"icon": "/img/misc/missing_icon_d2.png",
"description": "Increases the effective range of this weapon.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "Increases the effective range of this weapon.",
"id": 1240592695
}
,
{
"entityType": "DestinyStatDefinition",
"index": 21,
"icon": "/img/theme/destiny/icons/icon_magazineSize.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Magazine",
"ciName": "magazine",
"hash": 3871231066,
"displayProperties": {
"name": "Magazine",
"icon": "/img/theme/destiny/icons/icon_magazineSize.png",
"description": "The number of shots which can be fired before reloading.",
"hasIcon": true
},
"hasIcon": true,
"interpolate": false,
"description": "The number of shots which can be fired before reloading.",
"id": -423736230
}
,
{
"entityType": "DestinyStatDefinition",
"index": 14,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Impact",
"ciName": "impact",
"hash": 4043523819,
"displayProperties": {
"name": "Impact",
"icon": "/img/misc/missing_icon_d2.png",
"description": "Increases the damage inflicted by each round.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "Increases the damage inflicted by each round.",
"id": -251443477
}
,
{
"entityType": "DestinyStatDefinition",
"index": 24,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Reload Speed",
"ciName": "reload speed",
"hash": 4188031367,
"displayProperties": {
"name": "Reload Speed",
"icon": "/img/misc/missing_icon_d2.png",
"description": "The time it takes to reload this weapon.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "The time it takes to reload this weapon.",
"id": -106935929
}
,
{
"entityType": "DestinyStatDefinition",
"index": 13,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Rounds Per Minute",
"ciName": "rounds per minute",
"hash": 4284893193,
"displayProperties": {
"name": "Rounds Per Minute",
"icon": "/img/misc/missing_icon_d2.png",
"description": "The number of shots per minute this weapon can fire.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "The number of shots per minute this weapon can fire.",
"id": -10074103
}
]

之后:

[{
"entityType": "DestinyStatDefinition",
"index": 15,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Range",
"ciName": "range",
"hash": 1240592695,
"displayProperties": {
"name": "Range",
"icon": "/img/misc/missing_icon_d2.png",
"description": "Increases the effective range of this weapon.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "Increases the effective range of this weapon.",
"id": 1240592695
},
{
"entityType": "DestinyStatDefinition",
"index": 19,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Stability",
"ciName": "stability",
"hash": 155624089,
"displayProperties": {
"name": "Stability",
"icon": "/img/misc/missing_icon_d2.png",
"description": "How much or little recoil you will experience while firing the weapon.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "How much or little recoil you will experience while firing the weapon.",
"id": 155624089
},
{
"entityType": "DestinyStatDefinition",
"index": 23,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Handling",
"ciName": "handling",
"hash": 943549884,
"displayProperties": {
"name": "Handling",
"icon": "/img/misc/missing_icon_d2.png",
"description": "The speed with which the weapon can be readied and aimed.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "The speed with which the weapon can be readied and aimed.",
"id": 943549884
},
{
"entityType": "DestinyStatDefinition",
"index": 21,
"icon": "/img/theme/destiny/icons/icon_magazineSize.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Magazine",
"ciName": "magazine",
"hash": 3871231066,
"displayProperties": {
"name": "Magazine",
"icon": "/img/theme/destiny/icons/icon_magazineSize.png",
"description": "The number of shots which can be fired before reloading.",
"hasIcon": true
},
"hasIcon": true,
"interpolate": false,
"description": "The number of shots which can be fired before reloading.",
"id": -423736230
},
{
"entityType": "DestinyStatDefinition",
"index": 14,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Impact",
"ciName": "impact",
"hash": 4043523819,
"displayProperties": {
"name": "Impact",
"icon": "/img/misc/missing_icon_d2.png",
"description": "Increases the damage inflicted by each round.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "Increases the damage inflicted by each round.",
"id": -251443477
},
{
"entityType": "DestinyStatDefinition",
"index": 24,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Reload Speed",
"ciName": "reload speed",
"hash": 4188031367,
"displayProperties": {
"name": "Reload Speed",
"icon": "/img/misc/missing_icon_d2.png",
"description": "The time it takes to reload this weapon.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "The time it takes to reload this weapon.",
"id": -106935929
},
{
"entityType": "DestinyStatDefinition",
"index": 13,
"icon": "/img/misc/missing_icon_d2.png",
"hasComputedBlock": false,
"aggregationType": 2,
"redacted": false,
"name": "Rounds Per Minute",
"ciName": "rounds per minute",
"hash": 4284893193,
"displayProperties": {
"name": "Rounds Per Minute",
"icon": "/img/misc/missing_icon_d2.png",
"description": "The number of shots per minute this weapon can fire.",
"hasIcon": false
},
"hasIcon": false,
"interpolate": false,
"description": "The number of shots per minute this weapon can fire.",
"id": -10074103
}
]

"4043523819": 0, //impact

返回一个错误的值,这会导致您进行的检查出现问题。您可以将一个值添加到排序顺序对象statSortOrder中的所有值中。

代替:

if(a.hash && statSortOrder[a.hash.toString()] && b.hash && statSortOrder[b.hash.toString()]) {

你应该像这样对密钥的存在进行更精确的检查:

if(a.hash && a.hash.toString() in statSortOrder && b.hash && b.hash.toString() in statSortOrder) {

其次,由于else部分中的一个错误,混乱变得更加严重,您在其中输出:

" and statSortOrder[a.hash.toString()] = " + statSortOrder[a.hash.toString]

但你忘了实际调用toString,本应调用:

" and statSortOrder[a.hash.toString()] = " + statSortOrder[a.hash.toString()]

最新更新