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

151
Vistas
How can I count the number of true statements in a list?

I have a dynamic layout within a React Native view - that changes based on provided data for the particular data set. Ie there could be a provided Guide, Inventory, or Observation list that need displaying - but there may just be a combination of one, two or 3 of these things provided within any dataset.

I need to supply conditional styles / display object throughout the view based on the state of each provided data type.

Currently I have a series of checks that return true if the state is populated i.e:

const hasSiteGuide = () => {
  if (!this.isEmpty(this.state.locationSiteGuidePdf)) {
    return true;
  }
}; 

I then have a series of functions that determine how many items are supplied and provide the relevant style - here are the logic checks:

 const layout_full = () => {
  if (hasSiteGuide() && hasInventoryItems() && hasObservations()) {
    return true;
  }
};
const layout_one = () => {
  if (!hasSiteGuide() && !hasInventoryItems() && !hasObservations()) {
    return true;
  }
};

const layout_two = () => {
      if (!hasSiteGuide() || !hasInventoryItems() !hasObservations()) {
        if (hasSiteGuide() || hasInventoryItems()) {
          return true;
        }
      }
    };

the checks are used to display conditional styles in multiple areas - for example:

 <View
              style={[
                layout_full() && RP_Styles.btnWrap,
                layout_three() && RP_Styles.btnWrap_three,
                layout_two() && RP_Styles.btnWrap_two,
                layout_one() && RP_Styles.btnWrap_one,
              ]}>

My question is - i'm sure that there is a better way to do this.. particularly as i'm about to implement a 4th possibility - I was wondering if there is a way that I could use just one function to count how many true statements are in a list and return the relevant condition

ie:

returnTrueCount(hasSiteGuide(), hasInventoryItems(), hasObservations()). - is this possible? Or has anybody got an idea for a better approach please? Thanks very much

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

0

If you want do something like this : returnTrueCount(hasSiteGuide(), hasInventoryItems(), hasObservations()) then I think the following solution would would work.

const a = () => true;
const b = () => true;
const c = () => false;
const d = () => true;

function returnTrueCount(...funcs) {
  return funcs.reduce((a, f) => a + +f(), 0);
}

console.log(returnTrueCount(a, b, c, d));

about 4 years ago · Juan Pablo Isaza Denunciar

0

This worked for me:

    const returnTrueCount = (array, value) => {
      return array.filter((v) => v === value).length;
    };

called with a basic array and the value to check.

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