-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
Iterator in a loop is a 'string' instead of 'number' #39089
Comments
I guess I'm probably missing some logical reason as to why an iterator is a string but it just seems odd -- When I was writing up a quick sort algorithm, I had to do |
This is the way JavaScript works and Node.js can't realistically change it. You will get the same result running your code in a browser.
|
Option 1: If you don't want to create a new data structure from the existing data, you'll have to use a vanilla Option 2: You can instead iterate over both the keys and values using const movies = [
{
title: 'Requiem for a Dream',
duration: 128
}
]
function iteratorType(array) {
for (let [i, val] of array.entries()) {
console.log(typeof i)
}
}
iteratorType(movies) This option can be handy if you also need the value. |
I'm going to close this because there isn't anything we're likely to do to change this behavior. But feel free to leave additional comments/questions if we've missed something. |
Thank you all for the valuable feedback! |
Version: v12.16.1, v15.14.0 (tested)
Platform(s) (tested):
What steps will reproduce the bug?
Create test array and console log out the
typeof
of the iterator to see the resultSample snippet:
How often does it reproduce? Is there a required condition?
Every time. Conditions: An array with at least one value.
What is the expected behavior?
The iterator should be
typeof
number.What do you see instead?
A typeof of 'string' is returned
The text was updated successfully, but these errors were encountered: