Created
August 30, 2014 11:52
-
-
Save VanDalkvist/7d42cf021db285e096f8 to your computer and use it in GitHub Desktop.
map and merge arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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