Skip to content

Instantly share code, notes, and snippets.

@VanDalkvist
Created August 30, 2014 11:52
Show Gist options
  • Save VanDalkvist/7d42cf021db285e096f8 to your computer and use it in GitHub Desktop.
Save VanDalkvist/7d42cf021db285e096f8 to your computer and use it in GitHub Desktop.
map and merge arrays
function map(collection, callback) {
var index = -1,
length = collection ? collection.length : 0,
result = [];
if (angular.isArray(collection)) {
while (++index < length) {
var part = callback(collection[index], index, collection);
pushAll(result, part);
}
} else {
collection.forEach(function (value, key, collection) {
var part = callback(value, key, collection);
pushAll(result, part);
});
}
return result;
function pushAll(destination, source) {
if (angular.isArray(source)) {
source.forEach(function (elem) {
destination.push(elem);
});
} else {
destination.push(source);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment