Skip to content

Instantly share code, notes, and snippets.

@GustavoAdolfo
Created February 23, 2020 02:31
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 GustavoAdolfo/ba28c8f8bdfc73821d58c33ebccca67d to your computer and use it in GitHub Desktop.
Save GustavoAdolfo/ba28c8f8bdfc73821d58c33ebccca67d to your computer and use it in GitHub Desktop.
Filtrando e mapeando array de objetos em javascript
var arrayzao = [
[ //arrayzinho
{ id: 1, nome: 'abc', valor: 10.5 },
{ id: 2, nome: 'def', valor: 9.99 },
{ id: 3, nome: 'ghi', valor: 5.75 }
],
[ //arrayzinho
{ id: 1, nome: 'abc', valor: 11.5 },
{ id: 2, nome: 'def', valor: 10.0 },
{ id: 3, nome: 'ghi', valor: 7.0 },
{ id: 4, nome: 'jkl', valor: 8.0 }
],
];
function filtrar (arr, valor) {
if (!Array.isArray(arr)) {
return;
}
return arr.filter(elem => {
if (elem.id === valor) {
return elem;
}
});
}
var filtrados = arrayzao.map(item => filtrar(item, 3)[0]);
console.log('filtrados', filtrados);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment