简体   繁体   中英

Javascript function Object.keys not working correct

Javascript function Object.keys seems to not work correct:

public availableParents: any[] = [];

availableParents[abbreviation] = textField;

the field availableParents is sent to a function.

Then in debug mode I dispay the variable ... and also an Object.keys on the variable :

Immediate window :

?dataSource
[]
    __proto__: []
    ALG: "ALG | Alg"
    length: 0
    SC-1-1: "Scene"

? Object.keys(dataSource)
[SC-1-1,ALG]
    __proto__: []
    length: 2
    [0]: "SC-1-1"
    [1]: "ALG"

I would have expected that Object.keys would return (?) :

[ALG,SC-1-1]
    __proto__: []
    length: 2
    [0]: "ALG"
    [1]: "SC-1-1"

You can't order an object. The keys are stocked in (pseudo-)random order.

If you want to keep track of which key/value you put in first, you better have to use arrays.

If you just want to have an alphabetical order, use sort() method on your Object.keys resulting list

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM