为什么以下javascript数组排序决定我的两个排序顺序参数未定义?

时间:2017-10-14 17:07:32

标签: javascript arrays node.js sorting

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

此对象定义我想要使用的排序顺序。

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
  }
]

2 个答案:

答案 0 :(得分:3)

"4043523819": 0, //impact

返回一个假值,这会导致您所做的检查有问题。您可以在排序顺序对象statSortOrder中为所有值添加一个。

答案 1 :(得分:2)

而不是:

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()]