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

array function: repeat val x times #868

Closed
arieljake opened this issue Nov 21, 2012 · 4 comments
Closed

array function: repeat val x times #868

arieljake opened this issue Nov 21, 2012 · 4 comments

Comments

@arieljake
Copy link

suggested impl:

_.repeat("0",10)
// => ["0","0",... 10 times]

or alternatively, modify _.range function so that:

_.range(0,0,10), instead of outputting [] outputs the above.

Here is the use case:

var values = ["A","B","C"];
_.object(values)
// => {"A":undefined,"B":undefined,"C":undefined} // wonderful, as expected

But, what if i want to initialize every value in the key/value pairs to the same value?
This could be another option but doesn't work:

_.object(values,"0") // provide scalar instead of array
// => {"A":undefined,"B":undefined,"C":undefined} // same as before

This would work if _repeat existed:

_.object(values,_.repeat("0",values.length))
// => {"A":"0","B":"0","C":"0"} 
@fbuchinger
Copy link

+1 for an array repeat function. I wouldn't modify _.range for that, since it is dedicated to number ranges.

@jdalton
Copy link
Contributor

jdalton commented Nov 22, 2012

Instead of another method we could just mod _.times to return the result of each callback call, like Lo-Dash does:

var zeros = _.times(10, function() { return 0; });
// => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

@michaelficarra
Copy link
Collaborator

+1 @jdalton.

@jashkenas
Copy link
Owner

Sounds good to me -- go for it.

jashkenas added a commit that referenced this issue Nov 26, 2012
fixes #868: _.times collects return values
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

5 participants