2017-10-14 33 views
3

这是直接从我的代码和我的控制台日志中复制的。正如你所看到的,它是一个对象数组。我正在尝试按照我在单独对象中定义的排序顺序对对象进行排序。它对七个元素中的五个进行排序,但似乎任意决定排序对象的两个属性未定义。该对象通过我的代码进行了字符串处理,并在表示未定义的行之前写入日志。显然它不是未定义的。那么这里发生了什么?为什么下面的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 
 
    } 
 
]

+1

你的日志调用有一个错字 - 你已经有了'.toString',你应该有'的ToString()'。无论如何,真的不需要调用'.toString()',因为JavaScript总是对'']'运算符使用的表达式值隐式地执行。 – Pointy

+1

还要注意,你测试'statSortOrder'中的值的方式将意味着当散列映射到'0'时,它的行为就好像没有条目。你可以使用'in'运算符来避免这种情况,尽管这可能是故意的。 – Pointy

+0

谢谢Pointy。我更正了toString调用,但我认为结果仍然有效。起初我并没有给toString打电话,但它是其中一个问题,当你真的在摸你的脑袋时,你就开始尝试*任何东西*你知道。所以我绝望地加上了toString,但你的观点很好。 – Camenwolf

回答

3

"4043523819": 0, //impact 

返回falsy值,它是导致问题的那种你做检查的。您可以为排序顺序对象statSortOrder中的所有值添加一个值。

+0

...或者类似'(a.hash in statSortOrder)' – Pointy

+0

@Pointy,一个简短的版本将直接使用该值,如'return(statSortOrder [a.hash] || defaultValue) - (statSortOrder [b.hash] || defaultValue)',并根据需要采用'defaultValue'来将未经过哈希的值移动到底部或底部。 –

+0

这样做。非常感谢。 – Camenwolf

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