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

135
Vistas
Filter Object by key and value

Is there anyway to filter an object conditionally checking both the key and value? What I am trying to do is check if the key equals a string and check whether or not the value is empty.

I am trying to detect whether or not Teaching Hospital has an empty value, but Teaching Hospital Tax ID does not and vice-versa. If Teaching Hospital Name has a value, but Teaching Hospital Tax ID does not, the returned result should be: {Teaching Hospital Tax ID: ""}.

Object

{
  'Manufacturer Name': 'Ascendis',
  'HCP First Name': 'John',
  'HCP Middle Name': '',
  'HCP Last Name': '',
  'HCP NPI': 2442,
  'HCP Credentials': '',
  'HCP State License Number': '',
  'HCP State of Licensure': '',
  'HCP Specialty': '',
  'Teaching Hospital Name': 'Trinity',
  'Teaching Hospital Tax ID': '',
  'Address 1': '',
  'Address 2': '',
  'Postal Code': '',
  'Spend Date': '',
  'Spend Currency': '',
  'Spend Amount': '',
  'Form of Payment': '',
  'Nature of Payment': '',
  'Home System Identifier': '',
  'Product Name': '',
  'Travel City': '',
  'Travel State': '',
  'Travel Country': ''
}

Code

Object.entries(item)
          .filter(([key, value]) => (key === "Teaching Hospital Name" && value != "") && (key === "Teaching Hospital Tax ID" && value === ""))
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Use || instead of &&:

const obj = {
  'Manufacturer Name': 'Ascendis',
  'HCP First Name': 'John',
  'HCP Middle Name': '',
  'HCP Last Name': '',
  'HCP NPI': 2442,
  'HCP Credentials': '',
  'HCP State License Number': '',
  'HCP State of Licensure': '',
  'HCP Specialty': '',
  'Teaching Hospital Name': 'Trinity',
  'Teaching Hospital Tax ID': '',
  'Address 1': '',
  'Address 2': '',
  'Postal Code': '',
  'Spend Date': '',
  'Spend Currency': '',
  'Spend Amount': '',
  'Form of Payment': '',
  'Nature of Payment': '',
  'Home System Identifier': '',
  'Product Name': '',
  'Travel City': '',
  'Travel State': '',
  'Travel Country': ''
}

Object.entries(obj).forEach(([key, value]) => {
    if ((key === "Teaching Hospital Name" && value != "") || (key === "Teaching Hospital Tax ID" && value === "")) {
        delete obj[key]
    }
})

console.log(obj)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Array.prototype.reduce to create a new object with the select properties removed:

Object.entries(obj).reduce((acc, [key, value]) => {
  if (
    (key === "Teaching Hospital Name" && value !== "") ||
    (key === "Teaching Hospital Tax ID" && value === "")
  ) {
    // do not add key/value to resulting object
    return { ...acc };
  }

  return { ...acc, [key]: value };
}, {});

This avoids mutating the original object.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Lets analyse what we got and what we want.

  • We have a model obj.
  • We want pick specific keys and run specific check against picked keys/values
  • The check returns object with those keys which values are empty string
  • Additionally the check will ignore the results if all values are empty

We could write above as

1. if(allValues has an empty string) return false // ignore results
2. if(allValues has NOT an empty string) return false // ignore 3. results
3. return object with those keys where value is empty string

which in JS would looks like

// your model
const obj = {}
// your group of keys you want to pick from object
const group = [];
// picked [[key,value]] defined by your group
const entries = Object.entries(obj).filter(([key]) => group.includes(key));
// filter out those which are empty
const emptyEntries = entries.filter(([key, value]) => value === ""); 
// 1. step - to check if all values are empty we can check lengths
if(entries.length === emptyEntries.length) return false;
// 2. step - just check if filtered length is 0
if(emptyEntries.length === 0) return false
// 3. step - convert your emptyEntries to object
return Object.fromEntries(emptyEntries);

Lets see if below code works for you

const obj = {
  'Manufacturer Name': 'Ascendis',
  'HCP First Name': 'John',
  'HCP Middle Name': '',
  'HCP Last Name': '',
  'HCP NPI': 2442,
  'HCP Credentials': '',
  'HCP State License Number': '',
  'HCP State of Licensure': '',
  'HCP Specialty': '',
  'Teaching Hospital Name': 'Trinity',
  'Teaching Hospital Tax ID': '',
  'Address 1': '',
  'Address 2': '',
  'Postal Code': '',
  'Spend Date': '',
  'Spend Currency': '',
  'Spend Amount': '',
  'Form of Payment': '',
  'Nature of Payment': '',
  'Home System Identifier': '',
  'Product Name': '',
  'Travel City': '',
  'Travel State': 'nonempty',
  'Travel Country': ''
}
function check(obj, groupArr) {
  const group = new Set(groupArr);
  const vals = Object.entries(obj).filter(([key, value]) => group.has(key) && value === "");
  if(vals.length === group.size || vals.length === 0) return false;
  return Object.fromEntries(vals);
}
console.log(check(obj, ["Travel City", "Travel State", "Travel Country"]));
/* results
{
   "Travel City": "",
   "Travel Country": ""
}
*/

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