Skip to content

Instantly share code, notes, and snippets.

@ruanmer
Last active December 15, 2015 07:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruanmer/5221678 to your computer and use it in GitHub Desktop.
Save ruanmer/5221678 to your computer and use it in GitHub Desktop.
joinForEach
Array.prototype.joinForEach = function(separator, fn, scope){
var array = new Array();
for (var i = 0, len = this.length; i < len; ++i) {
fn.call(scope, array, this[i], i, this);
}
return array.join(separator);
};
var r, a = [{ name: 'Banana', url: '/Banana'}, { name: 'Abacaxi', url: '/Abacaxi'}, { name: 'Pera', url: '/Pera'}, { name: 'Tomate', url: '/Tomate'}];
r = a.joinForEach(', ', function(arr, item){
arr.push('<a href="'+ item.url +'">'+ item.name +'</a>');
});
console.log( r ); // "<a href="/Banana">Banana</a>, <a href="/Abacaxi">Abacaxi</a>, <a href="/Pera">Pera</a>, <a href="/Tomate">Tomate</a>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment