Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.entries and Object.values behave unexpectedly for objects with numeric keys #2375

Closed
philipandresen opened this issue Jun 1, 2023 · 1 comment

Comments

@philipandresen
Copy link

philipandresen commented Jun 1, 2023

I'm not sure about the theoretical minimal reproduction case but this is the best I have:

Espruino Web IDE - Left hand side:
Enter:
Object.entries({"1": "Hello"})
Expected result:
[ [ "1", "Hello" ] ]
Actual result:
[ [ "1", undefined ] ]

Enter:
Object.values({"1": "Hello"})
Expected result:
[ "Hello" ]
Actual result:
[ undefined ]

By contrast if you do these tests with an object containing "1a" as a key, it works as expected. That would be a reasonable workaround in some cases. Another workaround would be using Object.keys (which works fine) and then programmatically accessing the object within a loop, which is what I'll probably do for now.

Use case: Using an object as a map where the key is a numeric ID.

Suspected root cause: ae66489

I would normally try to submit a fix, but I am super shaky on C and am pretty new to Espruino and javascript interpreters in general. If I can work it out I would be happy to open a PR but it's a little over my head at the moment.

@gfwilliams
Copy link
Member

Thanks! That's super helpful.

A change went in recently to ensure that Object.keys/etc always returned array indexes as strings to be spec compliant, and I bet that broke this. Hopefully quite an easy fix though...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants