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

133
Vistas
How to ignore one key and validate all key for having non empty value

So I'm Learning javascripts array functions and found one solution too but it is using Object.fromEntries but in my angular project I have old es version and cant update it due to some reason.

so the problem is I have one array of object which is

var a =
    [{
        "dateOfDeposit": "2022-06-08T18:30:00.000Z",
        "cNumber": 44444,
        "code": "5555555",
        "amount": "5,555",
        "isTaxDetails": true,
        "id":""
    },
    {
        "dateOfDeposit": "2022-06-08T18:30:00.000Z",
        "cNumber": 45454,
        "code": "2121212",
        "amount": "",
        "isTaxDetails": true,
        "id":""
    }]

and I want to check all object should have value in all keys except key "id"

so I was using below code to achieve it

a.map((ele: any) => Object.fromEntries(
        Object.entries(ele)
          .filter(([key, val]) => key != "id" && val)
      ));

still I dont get the desired result as

    [{
        "dateOfDeposit": "2022-06-08T18:30:00.000Z",
        "cNumber": 44444,
        "code": "5555555",
        "amount": "5,555",
        "isTaxDetails": true,
        "id":""
    }]

below is the desired output

[{
        "dateOfDeposit": "2022-06-08T18:30:00.000Z",
        "cNumber": 44444,
        "code": "5555555",
        "amount": "5,555",
        "isTaxDetails": true,
        "id":""
    }]
    only one object bcz all key contains value expect id key

which is wrong. So any javascript function which can help?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use

var a =
    [{
        "dateOfDeposit": "2022-06-08T18:30:00.000Z",
        "cNumber": 44444,
        "code": "5555555",
        "amount": "5,555",
        "isTaxDetails": true,
        "id":""
    }, {
        "dateOfDeposit": "2022-06-08T18:30:00.000Z",
        "cNumber": 44444,
        "code": "5555555",
        "amount": "5,555",
        "isTaxDetails": null,
        "id":""
    },
{
        "dateOfDeposit": "2022-06-08T18:30:00.000Z",
        "cNumber": 44444,
        "code": "5555555",
        "amount": "5,555",
        "isTaxDetails": 0,
        "id":""
    }];
var result = a.filter(function(item){
   return Object.entries(item).every(function([key, val]){
        return key === "id" || (val != null && val !== "");
    })
})
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

IMO, you are doing it right with the first way (i.e. Object.fromEntries + Object.entries + Array.filter)

There is another way which is to use Object.entries + Array.reduce to reduce your entries array into an Object.

However, in my experience, the first solution you implemented always yielded better execution time in my case so I would stick with that.


Edit: From looking at your desired output and what your current input is, here is what you want to use.

var a = [
    {
        "dateOfDeposit": "2022-06-08T18:30:00.000Z",
        "cNumber": 44444,
        "code": "5555555",
        "amount": "5,555",
        "isTaxDetails": true,
        "id":""
    },
    {
        "dateOfDeposit": "2022-06-08T18:30:00.000Z",
        "cNumber": 45454,
        "code": "2121212",
        "amount": "",
        "isTaxDetails": true,
        "id":""
    },
];

var filteredArray = a.filter((item) => Object.entries(item).every(([key, value]) => key === "id" || typeof value != "string" || value.length > 0));

console.log(filteredArray);
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