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

123
Visualizações
Remove array of object if object inside is empty

I am preparing an array like this

 datas[5] = { "qty_sized": "1", "resolution": "5", "status": "", "order": 1342 };

where [5] is dynamic from response.

I have and object mydata and inside that I have a object items.

I push array to object items, with assign

Object.assign(mydatadata.items, datas);

Now mydata.items has an array set, `

items{
1 {qty_auth: "", resolution: "4", status: "", order: "1495"},
5 {qty_sized: "1", resolution: "4", status: "", order: "1485"}
}`

Now if qty_auth: "" , from which i need to check if qty_ is empty then remove the array . So expected output is something like this: Note: qty_ is dynamic here.

items{ 5 {qty_sized: "1", resolution: "4", status: "", order: "1485"} }

and i want to result inside same object mydata.items

I tried something like this

const mydatadata.items  = mydata.items.filter((o) =>
            Object.keys(o).some((k) => k.startsWith("qty") && o[k])
);

console.log(result);

but its now giving me any output

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

0

  • Using Object#entries, get the key-value pairs of items
  • Using Array#filter, iterate over the above array
  • In each iteration, check if the current item has a key starting with qty_ whose value is not empty. You can do this using Object#keys, Array#some, and String#startsWith.
  • Using Object#fromEntries, convert the filtered pairs to an object again.

const obj = {
  items: {
    1: {qty_auth: "", resolution: "4", status: "", order: "1495"},
    5: {qty_sized: "1", resolution: "4", status: "", order: "1485"}
  }
};

obj.items = Object.fromEntries(
  Object.entries(obj.items)
  .filter(([_, item]) =>
    Object.keys(item).some(key => key.startsWith('qty_') && item[key])
  )
);

console.log(obj);

about 4 years ago · Juan Pablo Isaza Relatório

0

You're talking about an array, but using curly brackets instead of square brackets. For filter() to work it would have to look like:

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

Assuming it is an actual array there's still a problem with "const mydatadata.items", or at least it throws an error for me because mydatadata is not initialized. Unless it's a typo and it should be mydata, but then you'd be redeclaring it. So depending on what you want:

mydata.items = mydata.items.filter((o) =>
        Object.keys(o).some((k) => k.startsWith("qty") && o[k])
);

or

let mydatadata = {};
mydatadata.items = mydata.items.filter((o) =>
        Object.keys(o).some((k) => k.startsWith("qty") && o[k])
);

Furthermore you're storing the result in mydatadata but you're logging the variable result. So depending on the previous answer:

console.log(mydatadata);

or

console.log(mydata);

Here's a fiddle: https://jsfiddle.net/b57qa82d/

about 4 years ago · Juan Pablo Isaza Relatório

0

You should probably just be using an array rather than an object. It's not really clear from your question what structure you need as you keep changing the terminology to describe your data. For example: "Now mydata.items has an array set" but your code says that it should be object with keys, not an array.

So I suggest: get your data in an array, and filter it by iterating over each object's entries and checking to see if any of the keys starting with "qty" has a value that isn't an empty string. You can then assign that filtered array to myObject.items.

const data = [
{ "qty_sized": "0", "resolution": "5", "status": "", "order": 2 },
{ "qty_auth": "", "resolution": "5", "status": "", "order": 3 },
{ "qty_auth": "1", "resolution": "5", "status": "", "order": 1342 },
{ "qty_sized": "", "resolution": "2", "status": "", "order": 1 },
{ "qty_sized": "1", "resolution": "1", "status": "", "order": 142 }];

const filtered = data.filter(obj => {
  return Object.entries(obj).find(([key, value]) => {
    return key.startsWith('qty') && value;
  });
});

const myObject = { items: filtered };

console.log(myObject);

Additional documentation

  • Object.entries

  • find

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