Dado que tengo el siguiente script, ¿cómo puedo contar solo si el elemento de la matriz tiene el valor definido? En el siguiente escenario, obtengo 3 porque cuenta las matrices, pero ¿cómo verifico si el atributo real de la matriz de firstName o lastname es no está vacío, quiero que cuente si se completa el nombre o el apellido.
var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}] var noGuests = 0 for (a in x) { noGuests++ } alert(noGuests)actualizar El motor de renderizado javascript de mi aplicación está desactualizado y no puede usar los filtros.
Mono Araña 1.8.5
Simplemente puede filtrar la matriz por elemento que item.firstName y item.lastName no están vacíos y obtener la length de la matriz de resultados
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);Si se completa el nombre o el apellido, esto contará. Si necesita tanto el nombre como el apellido, cambie la condición de || a && .
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);O puede hacerlo usando 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);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 ) || []; } 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