2017-10-14 2 views
3

Ceci est copié directement à partir de mon code et de mon journal de console. Comme vous pouvez le voir, c'est un tableau d'objets. J'essaie de trier les objets en fonction de l'ordre de tri que j'ai défini dans un objet séparé. Il trie 5 des sept éléments mais apparemment arbitrairement il décide que deux des propriétés de l'objet de tri ne sont pas définies. Cet objet est stringifié par mon code et écrit dans le journal juste avant la ligne qui indique qu'il n'est pas défini. Clairement, ce n'est pas indéfini. Alors qu'est-ce qui se passe ici?Pourquoi le tri par matrice javascript suivant détermine-t-il que deux de mes paramètres d'ordre de tri ne sont pas définis?

Cet objet définit l'ordre de tri que je souhaite utiliser.

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

Ce code doit trier le tableau d'objets en fonction de cet ordre. Notez que j'ai quelques trucs supplémentaires ici pour cracher des résultats détaillés de ce qui arrive à la console. Je vais copier le contenu du journal résultant et le contenu du tableau "orderArray" plus bas.

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)); 

Voici donc ce que je capture dans mon journal:

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 

Et voici le contenu du tableau d'objets à la fois avant et après cette opération de tri:

Avant:

[ 
 
{ 
 
    "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 
 
} 
 
]

Après:

[{ 
 
    "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

Votre appel d'enregistrement a une faute de frappe - vous avez '.toString' où vous devriez avoir' .toString() '. De toute façon, il n'est pas nécessaire d'appeler '.toString()', car JavaScript le fait implicitement pour une valeur d'expression utilisée avec l'opérateur '[]'. – Pointy

+1

Notez également que la façon dont vous testez une valeur dans 'statSortOrder' signifie que lorsque le hash est mappé sur' 0', il se comporte comme s'il n'y avait pas d'entrée. Vous pouvez utiliser l'opérateur 'in' pour éviter cela, bien que ce soit intentionnel. – Pointy

+0

Merci point. J'ai corrigé l'appel toString, même si je pense que les résultats sont toujours valables. Au départ je n'appelais pas à String, mais c'était une de ces choses où quand un problème vous gratte vraiment la tête, vous commencez juste à essayer * n'importe quoi * tu sais. J'ai donc ajouté à Sortir de la désertion, mais votre point est bien pris. – Camenwolf

Répondre

3

Ce

"4043523819": 0, //impact 

retourne une valeur falsy et il est à l'origine d'un problème avec le type de contrôles que vous faites. Vous pouvez ajouter une à toutes les valeurs dans l'objet d'ordre de tri statSortOrder.

+0

... ou faire quelque chose comme '(a.hash dans statSortOrder)' – Pointy

+0

@Pointy, une version courte serait d'utiliser la valeur directement, comme 'return (statSortOrder [a.hash] || defaultValue) - (statSortOrder [b.hash] || defaultValue) 'et prend un' defaultValue' pour déplacer les valeurs non hachées vers ou en bas, comme désiré. –

+0

Ça l'a fait. Merci beaucoup. – Camenwolf

2

Au lieu de:

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

... vous devriez faire un contrôle plus précis pour l'existence clé comme ceci:

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

En second lieu, la confusion se fait plus à cause d'un bogue dans la else partie, où vous sortie:

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

... mais vous avez oublié d'appeler réellement toString et aurait dû faire:

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