Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda