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

526
Visualizações
Remove from array of Object if Empty

I have a array of object ,

var  arr = [
  {qty_auth: "", resolution: "4", status: "", order: "1495"},
  {qty_sized: "1", resolution: "4", status: "", order: "1485"}
]

If first one is empty (ex:qty_auth),want to remove the object from the array on loop. The First one is dynamic key as qt_auth,qty_sized is dynamic

So the output must be

var  arr = [
  {qty_sized: "1", resolution: "4", status: "", order: "1495"}
]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

There is not particular sequence in an object in JS, but what you can do is check for the existence and filter out only the object that contain qty_auth or qty_sized and it should not be empty "". You can use filter

var arr = [
  { qty_auth: "", resolution: "4", status: "", order: "1495" },
  { qty_sized: "1", resolution: "4", status: "", order: "1485" },
];

const result = arr.filter((o) => o.qty_auth || o.qty_sized);
console.log(result);

EDITED: If you want to filter the objects which starts with qty and which is empty then you can do as:

var arr = [
  { qty_auth: "", resolution: "4", status: "", order: "1495" },
  { qty_sized: "1", resolution: "4", status: "", order: "1485" },
];

const result = arr.filter((o) =>
  Object.keys(o).some((k) => k.startsWith("qty") && o[k])
);
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

  • Using Array#filter, iterate over the array
  • In each iteration, check if the current object has a key starting with qty_. You can do this using Object#keys, Array#find, and String#startsWith. If the key exists but its value is empty, return false

const arr = [ {qty_auth: "", resolution: "4", status: "", order: "1495"}, {qty_sized: "1", resolution: "4", status: "", order: "1485"} ];

const res = arr.filter(e => {
  const qtyKey = Object.keys(e).find(key => key.startsWith('qty_'));
  if(qtyKey && !e[qtyKey]) return false;
  return true;
});

console.log(res);

Note: the key order in javascript objects is undefined.

about 4 years ago · Juan Pablo Isaza Relatório

0

As @EnricoMassone below pointed out, the order of objects entries here, which I iterate once I have my object from the array (the forEach loop) might not be the same as declared, so it may be a controversial method. FYI I check if the first object property is not empty and if so, I add the whole object to the array I return.

const removeEmpty = (arr) => {
    const arrToReturn = new Array();

    arr.forEach((obj) => {
        if (Object.entries(obj)[0][1] === '') continue;
        else arrToReturn.push(obj);
    });

    return arrToReturn;
}
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