Skip to content

Instantly share code, notes, and snippets.

@darlanalves
Forked from VictorQueiroz/angularjs-join-filter.js
Last active August 29, 2015 14:02
Show Gist options
  • Save darlanalves/5b0865b7e3c8e3b00b67 to your computer and use it in GitHub Desktop.
Save darlanalves/5b0865b7e3c8e3b00b67 to your computer and use it in GitHub Desktop.
/*
* Exemplo:
* var users = [
* {name: 'Victor Queiroz'},
* {name: 'João Bosco'},
* {name: 'Ruan Jordão'}
* ];
*
* Aplicando o filtro:
* {{ users | pluck:'name' | join:', ' }}
*
* Retorno:
* Victor Queiroz, João Bosco, Ruan Jordão
*
* Dependências:
* - VanillaJS
*/
angular.module('filters', [])
.filter('join', function() {
return function(list, token) {
return (list||[]).join(token);
}
}
.filter('pluck', function() {
function pluck(objects, property) {
if (!(objects && property && angular.isArray(objects))) return [];
property = String(property);
return objects.map(function(object) {
// just in case
object = Object(object);
if (object.hasOwnProperty(property)) {
return object[property];
}
return '';
});
}
return function(objects, property) {
return pluck(objects, property);
}
});
});
@VictorQueiroz
Copy link

So fucking much better, congratulations! And thank you!

@alexandrricov
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment