It's a little different from Object.keys and so on. You can do Object.keys(arr) too, but then you'll get an array of string keys, rather than an iterator of number keys. Interestingly, there's no such thing as Object#keys — I guess it doesn't make sense to return an iterator of keys for something that itself isn't iterable.
Right, because they're class methods on Object that take a param, that's probably why I didn't think to look into how it could work with arrays. Very interesting.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You can also use
Array#keys
to get an iterator with the array indexes as numbers:Similarly,
Array#values
gives the elements of the array, andArray#entries
gives tuples of[index, element]
:Awesome! I didn't put 2 + 2 that
.keys()
and.entries()
would work on arrays (I use them all the time for objects), that's great.It's a little different from
Object.keys
and so on. You can doObject.keys(arr)
too, but then you'll get an array of string keys, rather than an iterator of number keys. Interestingly, there's no such thing asObject#keys
— I guess it doesn't make sense to return an iterator of keys for something that itself isn't iterable.Right, because they're class methods on Object that take a param, that's probably why I didn't think to look into how it could work with arrays. Very interesting.