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)
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?– Woss
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 asconst listalunos = [pessoa("maria", 20, 7), pessoa("joao", 23, 5), etc];
– Isac