Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
How to filter an array of JavaScript objects?

Lets say I have the following object

var ob = [
  {
    "id": 1,
    "comment": "some comment",
    "hidden": 0,
  },
  {
    "id": 2,
    "comment": "some comment",
    "hidden": 1,
  },
  {
    "id": 3,
    "comment": "some comment",
    "hidden": 1,
  },
  {
    "id": 4,
    "comment": "some comment",
    "hidden": 0,
  }
]

How can I filter this to say for example return all where hidden = 1

Cheers

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Use Array#filter:

let arr = [
  {
    "id": 1,
    "comment": "some comment",
    "hidden": 0,
  },
  {
    "id": 2,
    "comment": "some comment",
    "hidden": 1,
  },
  {
    "id": 3,
    "comment": "some comment",
    "hidden": 1,
  },
  {
    "id": 4,
    "comment": "some comment",
    "hidden": 0,
  }
];
let res = arr.filter(x => x.hidden === 1);
// or arr.filter(({hidden}) => hidden === 1);
console.log(res);

about 4 years ago · Juan Pablo Isaza Relatório

0

var ob = [
  {
    "id": 1,
    "comment": "some comment",
    "hidden": 0,
  },
  {
    "id": 2,
    "comment": "some comment",
    "hidden": 1,
  },
  {
    "id": 3,
    "comment": "some comment",
    "hidden": 1,
  },
  {
    "id": 4,
    "comment": "some comment",
    "hidden": 0,
  },
  {
    "id": 5,
    "comment": "some comment",
    "hidden": 2,
  }
]

let result = ob.filter(x => x.hidden === 1);
console.log("result for hidden properties 1",result)
//Or you can use other condision. Just think like conditional operator but this is kind of different thing 
let result2 = ob.filter(x => x.hidden === 2);
console.log("result for hidden properties 2",result2)
//This condition only pass when hidden properties values 2. otherwise It won't filter and won't give your expected output , or result whatever you say.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda