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

228
Vistas
Javascript: object array, check if item has value

given that I have the following script, how can I only count if the array item has the value defined, in the following scenario i get 3 because it counts the arrays, but how do I check if the actual array attribute of firstName or lastname is not empty, I want it to count if either first name or last name is populated.

var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}]

var noGuests = 0
for (a in x) {
 noGuests++
}    
alert(noGuests)

update My application's javascript render engine is outdated and can't use filter's.

SpiderMonkey 1.8.5

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

0

You can just filter the array by item which item.firstName and item.lastName is not empty and get the length of the result array

var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}]

const result = x.filter(item => item.firstName && item.lastName).length;

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

If any of the firstName or lastName is populated then this will count. If you need both firstName and lastName then change the condition from || to &&.

var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}]

var noGuests = 0
for (a in x) {
 if (x[a]['firstName'] || x[a]['lastName']) {
    noGuests++
 }
}

console.log(noGuests);

Or can do so using forEach loop

var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}]

var noGuests = 0
x.forEach(x => {
  if (x.firstName || x.lastName) noGuests++;
})

console.log(noGuests);
about 4 years ago · Juan Pablo Isaza Denunciar

0

function count_non_empty(collection) {
  return filter_non_empty(collection).length;
}

function filter_non_empty(collection) {
  return collection
    && collection.length
    && collection.filter(elem =>
      (elem.firstName || '').trim() // <--- If firstName is empty or empty-like string
      || (elem.lastName || '').trim()  // <--- If lastName is empty or empty-like string
    ) || [];
}

Illustration

function count_non_empty(collection) {
  return filter_non_empty(collection).length;
}

function filter_non_empty(collection) {
  return collection &&
    collection.length &&
    collection.filter(elem =>
      (elem.firstName || '').trim() // <--- If firstName is empty or empty-like string
      ||
      (elem.lastName || '').trim() // <--- If lastName is empty or empty-like string
    ) || [];
}

let onlyLastName = {
  "firstName": "",
  "lastName": "SomeLastName"
};
let onlyFirstName = {
  "firstName": "SomeFirstName",
  "lastName": ""
};
let emptyLookingFirstName = {
  "firstName": "   ",
  "lastName": "SomeLastName"
};
let emptyLookingLastName = {
  "firstName": "  SomeFIrstName ",
  "lastName": "   "
};
let havingBoth = {
  "firstName": "guest1fnamee",
  "lastName": "guest1fnamee"
};
let notHavingAny = {
  "firstName": "   ",
  "lastName": ""
}

let x = [{
    "firstName": "guest1fnamee",
    "lastName": "guest1fnamee"
  },
  {
    "firstName": "guest2fnamee",
    "lastName": "guest2lnamee"
  },
  {
    "firstName": "   ",
    "lastName": ""
  }
];

console.log("Count - x:", count_non_empty(x));
console.log("Count:", count_non_empty([onlyLastName, onlyFirstName, havingBoth]));
console.log("Count:", count_non_empty([onlyLastName, onlyFirstName, havingBoth, emptyLookingFirstName]));
console.log("Count:", count_non_empty([onlyLastName, onlyFirstName, havingBoth, emptyLookingLastName]));
console.log("Count - Not Having Any:", count_non_empty([notHavingAny]));
console.log("Count - Having Both:", count_non_empty([havingBoth]));
console.log("Count - Not Having Any with Having Both:", count_non_empty([notHavingAny, havingBoth]));
console.log("Count - Not Having Any with Empty Looking First Name:", count_non_empty([notHavingAny, emptyLookingFirstName]));
console.log("Count - Not Having Any with Empty Looking Last Name:", count_non_empty([notHavingAny, emptyLookingLastName]));


WYSIWYG => WHAT YOU SHOW IS WHAT YOU GET

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