How to filter object

Asked

Viewed 429 times

0

I simply want to filter students with grades above or equal to 7 and show on console.log

const pessoa = (nome, idade, nota) =>{
    return {
        nome,
        idade,
        nota,
    }
}

const listalunos = [
 {aluno1: pessoa("maria", 20, 7)},
 {aluno2: pessoa("joao", 23, 5)},
 {aluno3: pessoa("pedro", 15, 10)},
 {aluno4: pessoa("eduardo", 24, 9)},
]


const notas = listalunos.filter(function(x){
    return x >= 7
})

console.log(listalunos.length)
console.log(listalunos.nota)
console.log(notas)
  • 5

    Could explain why your list is a list of objects where each object defines a different key to store the object pessoa? Why isn’t it just a list of people?

  • 3

    This definition of the student list has no meaning, and ends up complicating something simple, which could just be const notas = listalunos.filter(x => x.nota >= 7), provided that it is defined as const listalunos = [pessoa("maria", 20, 7), pessoa("joao", 23, 5), etc];

2 answers

3

The x in your filter returns each object of the listalunos, then there’s no way of knowing if the note inside the object is greater than or equal to 7 just with x >= 7.

You have to access the key nota within each object in the x. You can do it using Object.keys(x). Will return arrays with the names of the objects in x:

['aluno1']
['aluno2']
['aluno3']
...

So just take the first and only index of each array:

Object.keys(x)[0]

And apply to the filter by accessing the key nota within each object of x:

return x[Object.keys(x)[0]].nota >= 7

Example:

const pessoa = (nome, idade, nota) =>{ return { nome, idade, nota, } }

const listalunos = [ {aluno1: pessoa("maria", 20, 7)}, {aluno2: pessoa("joao", 23, 5)}, {aluno3: pessoa("pedro", 15, 10)}, {aluno4: pessoa("eduardo", 24, 9)}, ]

const notas = listalunos.filter(function(x){ return x[Object.keys(x)[0]].nota >= 7 })

console.log(notas)

Another way would be, if the names of the objects are in sequence (aluno1, aluno2, aluno3 etc...) is to take the second argument of the filter that returns the index of the objects (starting from 0), add +1 and concatenate with the "student":

const pessoa = (nome, idade, nota) =>{ return { nome, idade, nota, } }

const listalunos = [ {aluno1: pessoa("maria", 20, 7)}, {aluno2: pessoa("joao", 23, 5)}, {aluno3: pessoa("pedro", 15, 10)}, {aluno4: pessoa("eduardo", 24, 9)}, ]

const notas = listalunos.filter(function(x,i){ return x["aluno"+ (i+1)].nota >= 7 })

console.log(notas)

2


I see no point in defining a list of objects with the keys aluno1, aluno2, aluno3, etc, precisely because you will not have direct control over which key to access in each object.

For example, if I need the student’s name at position 5 on the list, as I would?

listalunos[5][???].nome

Ah, but if it’s in position 5, it must be aluno5, then listalunos[5]["aluno5"].nome, but what if it isn’t? What if the object with "aluno5" isn’t even on the list anymore?

You don’t need this key, just create a list of people:

const pessoa = (nome, idade, nota) =>{
    return {
        nome,
        idade,
        nota,
    }
}

const alunos = [
  pessoa("maria", 20, 7),
  pessoa("joao", 23, 5),
  pessoa("pedro", 15, 10),
  pessoa("eduardo", 24, 9),
]

And with this make the filter you want becomes trivial:

const aprovados = alunos.filter(aluno => aluno.nota >= 7)

You don’t need all the effort to solve a problem that you yourself entered.

const pessoa = (nome, idade, nota) =>{
    return {
        nome,
        idade,
        nota,
    }
}

const alunos = [
  pessoa("maria", 20, 7),
  pessoa("joao", 23, 5),
  pessoa("pedro", 15, 10),
  pessoa("eduardo", 24, 9),
]

const aprovados = alunos.filter(aluno => aluno.nota >= 7)

console.log(aprovados)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.