Skip to content

Instantly share code, notes, and snippets.

@mbjordan
Last active December 21, 2015 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbjordan/6221308 to your computer and use it in GitHub Desktop.
Save mbjordan/6221308 to your computer and use it in GitHub Desktop.
function listOrArray(Arr) {
var List = Array.prototype.slice.call(arguments, 1),
i;
// If `List` is not empty, assume there are more module objects sent as List Arguments
if (List.length > 0) {
List.unshift(Arr);
Arr = List;
} else {
// No List args in sight, treat the first arg `Arr` as an array and loop through it.
// Even if there's just one.
Arr = [].concat(Arr);
}
// Loop through that array
for (i in Arr) {
if (Arr.hasOwnProperty(i)) {
// Do something
}
}
}

List or Array

This function can take multiple arguments, either as a List or an Array.

listOrArray(1, 2, 3);// List
listOrArray(["a", "b", "c"]);// Array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment