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

_.pairs and maps #2517

Closed
jdalton opened this issue Apr 25, 2016 · 9 comments
Closed

_.pairs and maps #2517

jdalton opened this issue Apr 25, 2016 · 9 comments

Comments

@jdalton
Copy link
Contributor

jdalton commented Apr 25, 2016

I was working with maps and reached for a _.pairs method to get its key-value pairs and thought it would be handy to support.

var m = new Map([['a', 1], ['b', 2]]);
_.pairs(m);
// => [['a', 1], ['b', 2]]
@penDerGraft
Copy link

I would be up for working on this. Are you thinking this would just add a for..of loop in the existing _.pairs method, assuming it is an instanceof Map?

@jdalton
Copy link
Contributor Author

jdalton commented May 2, 2016

Yep! I was thinking just for maps at the moment using _.isMap.

penDerGraft added a commit to penDerGraft/underscore that referenced this issue May 2, 2016
add support for maps within _.pairs method by returning array of map entries
@penDerGraft
Copy link

penDerGraft commented May 2, 2016

Looks like there are several compatibility issues with different versions of node. in v0.10.* and below, there's an error parsing for..of. in v.0.12.*, Map is defined, but Array.from is not supported.

I think the best way to handle it is to iterate from 0 to map.size and push entries().next().value into the return array. Any other ideas?

@jdalton
Copy link
Contributor Author

jdalton commented May 2, 2016

fyi I created poll to see what folks expected the behavior of maps to be.
Results are ~50/50.

@jridgewell
Copy link
Collaborator

Why would it be []?

@jdalton
Copy link
Contributor Author

jdalton commented May 2, 2016

That's what the current draft spec has as its behavior.
It just iterates over an object's own enumerable keys.

@jridgewell
Copy link
Collaborator

Well, I should have read the spec. I was expecting something like the Reflect API, were it's just what Map#entries does.

@jdalton
Copy link
Contributor Author

jdalton commented May 2, 2016

Ya, I think it's kinda goofy for a method called entries to not return a map's entries.

@jgonggrijp
Copy link
Collaborator

Special case of #2147.

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

No branches or pull requests

4 participants