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

标签 javascript arrays node.js sorting

这是直接从我的代码和控制台日志中复制的。如您所见,它是一个对象数组。我正在尝试根据我在单独对象中定义的排序顺序对对象进行排序。它对 7 个元素中的 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
  }
]

最佳答案

这个

"4043523819": 0, //impact

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

关于javascript - 为什么以下 javascript 数组排序决定我的两个排序顺序参数未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46747181/

相关文章:

javascript - 点击事件中无法正确获取选中的radio

javascript - 获取文本范围的 offsetLeft

javascript - 如何在 Javascript 中访问数组中构造对象的属性

javascript - 需要: access/list all loaded modules

javascript - 为什么 JavaScript "this"在 Node 和 Browser 环境中返回不同的值?

javascript - 'formatted_address' 对于某些地方为 null,但并非全部(Google map API)这是为什么?

javascript - 在 React Native 中获取图像时 require() 不使用内插字符串

c - Qsort 按字母顺序排列的字符串数组

c - 访问全局数组会导致段错误

Node.JS selenium 发送 key 回车