How to go through an object and filter this object?

Asked

Viewed 916 times

0

I’m trying to go through an object and after that, filter the same.

I’m using this code, but it’s not working.

Someone could help me and if possible explain the code ?

const objeto = { 
idade: 20,
idade:20,
idade:21};

const funcao = parametros => parametros === 20;

const funcaooriginal = objeto.forEach(funcao);

console.log(funcaooriginal);

  • Your object makes no sense. It has three times the same attribute with different values. Make sure you don’t have one array of objects? Something like [{idade: 20}, {idade: 21}, ...]?

  • Actually yes, it was supposed to be: and each age, have a name

  • Then edit the question and enter the correct code, please.

1 answer

3

You can use the Array.prototype.filter

const lista = [
  { idade: 20, nome: 'Fulano' },
  { idade: 20, nome: 'Ciclano' },
  { idade: 21, nome: 'Beltrano' }
];

let filtrado = lista.filter((item) => item.idade == 20);
console.log(filtrado);

Browser other questions tagged

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