Created
February 23, 2020 02:31
-
-
Save GustavoAdolfo/ba28c8f8bdfc73821d58c33ebccca67d to your computer and use it in GitHub Desktop.
Filtrando e mapeando array de objetos em javascript
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
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