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

how to create object with keys from array, and values from a function? #1261

Closed
davidmaxwaterman opened this issue Jun 4, 2015 · 4 comments
Labels

Comments

@davidmaxwaterman
Copy link

I'm looking for something like this :

myObject = _.zipToObject([a, b, c], function () { return makeObject() });
// myObject -> { a: [Object], b: [Object], c: [Object] };

IE, it creates an object whose keys are taken from the array, and for each key, the value is set by a callback.

@jdalton
Copy link
Member

jdalton commented Jun 4, 2015

You can combo _.map or _.times and _.zipObject:

_.zipObject(keys, _.map(keys, function() { }));

@BlueRaja
Copy link

BlueRaja commented Oct 8, 2016

I've created #2718 which solves the above problem in a much more generic way

@xyzdata
Copy link

xyzdata commented Aug 15, 2017

https://stackoverflow.com/a/45687156/8202629

@lock
Copy link

lock bot commented Dec 27, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

4 participants