Question:
Why am I not able to return only the objects that contain the Matemática
discipline?
I'm using map()
, filter()
and, inside filter()
, includes()
, but it's returning all objects.
Is it not possible to use chained as I did, having to trigger the methods:
let dados = [ {nome: 'Noah', disciplinas: ['Matemática', 'Geografia', 'Inglês']}, {nome: 'Gael', disciplinas: ['Química', 'Geografia', 'Português']}, {nome: 'Caleb', disciplinas: ['Matemática', 'Física', 'Artes']} ] let discip = dados.filter(a => a.disciplinas.filter(b => b.includes('Matemática'))) console.log(discip)
Answer:
When you want to filter something, just do it once. It would only make sense if you wanted to have a filter within the filter, but that's not the case, you want to filter only people. Discipline is a criterion used not another filter (as I understand it). So just take the list of subjects and see if the one you are looking for is among those that the person has in their registration, in a direct and simple way.
let dados = [ {nome: 'Noah', disciplinas: ['Matemática', 'Geografia', 'Inglês']}, {nome: 'Gael', disciplinas: ['Química', 'Geografia', 'Português']}, {nome: 'Caleb', disciplinas: ['Matemática', 'Física', 'Artes']} ] let discip = dados.filter(a => a.disciplinas.includes('Matemática')) console.log(discip)
I put it on GitHub for future reference .