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

128
Visualizações
How to filter the nested object based on the status INVALID
    var testObject = {
      address: { form: 1, status: "VALID" },
      address2: {form: 1,  status: "INVALID" },
      address3: {form: 1,  status: "INVALID" }
    }
  Object.keys(testObject)
  .filter(key => console.log(key))

What I'm trying to do here is to filter the data based on the status INVALID.

example output should be like this.

{
  address2: { form: 1, status: "INVALID" },
  address3: { form: 1, status: "INVALID" }
}

or

[
  { address2: { form: 1, status: "INVALID" } },
  { address3: { form: 1, status: "INVALID" } }
]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

One liner:

Object.fromEntries(Object.entries(testObject).filter(x => x[1].status === "INVALID"))

Long:

const objectConvertedToArray = Object.entries(testObject)
const filteredArray = objectConvertedToArray.filter(([key, val] => val === "INVALID"))
const filteredObject = Object.fromEntries(filteredArray)
about 4 years ago · Juan Pablo Isaza Relatório

0

Loop over the Object.entries and add the properties that match to a new object.

var testObject = {
  address: { form: 1, status: "VALID" },
  address2: { form: 1,  status: "INVALID" },
  address3: { form: 1,  status: "INVALID" }
}

const out = {};

for (let [key, obj] of Object.entries(testObject)) {
  if (obj.status === 'INVALID') out[key] = obj;
}

console.log(out);

If you want an array push the object on to an array using the key.

var testObject = {
  address: { form: 1, status: "VALID" },
  address2: { form: 1,  status: "INVALID" },
  address3: { form: 1,  status: "INVALID" }
}

const out = [];

for (let [key, obj] of Object.entries(testObject)) {
  if (obj.status === 'INVALID') out.push({ [key]: obj });
}

console.log(out);

about 4 years ago · Juan Pablo Isaza Relatório

0

This should do the trick without changing the original object:

var testObject = {
  address: { form: 1, status: "VALID" },
  address2: {form: 1,  status: "INVALID" },
  address3: {form: 1,  status: "INVALID" }
}

var ouput = Object.keys(testObject).reduce((obj, element) => {
  if(testObject[element].status === 'INVALID') obj[element] = testObject[element]

  return obj
}, {})

console.log(ouput)

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