0

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"
6
  • It does not look it is the syntax of javascript, what are you working on ? Commented Jan 15, 2020 at 14:56
  • availableParents = empty list... I've never felt so sad reading code... Commented Jan 15, 2020 at 14:56
  • It's not clear to me what the issue is here. Commented Jan 15, 2020 at 14:59
  • 2
    Generally when you start thinking "hmm this widely-deployed programming language has a bug", you need to take a step back and re-think. Commented Jan 15, 2020 at 15:01
  • it's typescript. Commented Jan 15, 2020 at 21:26

1 Answer 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.