-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add _.chunk to Array functions. #1919
Changes from 1 commit
e917a18
db7f405
750f22c
dbc7375
e3035ba
63652f6
a59a6f9
7d4034c
ce3a67a
0c6d140
185abf6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -635,6 +635,19 @@ | |
|
||
return range; | ||
}; | ||
|
||
// Split an **array** into several arrays containing **int** or less elements | ||
// of initial array | ||
_.chunk = function(array, int) { | ||
var result = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, I wonder if it's faster to compute the length |
||
|
||
if (int <= 0) return []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or if |
||
if (int >= _.size(array)) return array; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
|
||
while (_.size(array) > 0) result.push(array.splice(0, int)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't modify the original array, do smt like for (var i = 0, length = array.length; i < length; i += int) {
result.push(array.slice(i, i + int);
} |
||
|
||
return result; | ||
}; | ||
|
||
// Function (ahem) Functions | ||
// ------------------ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
int
is a terrible variable name and I think may be a reserved word in ES3, use chunk or something