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

177
Vistas
Filter true meaning in javascript

If at least one of the values returned from the loop is true, the function must return true. How can I do this?

  const addCart = () => {
    for (let i = 0; i < props.cart.length; i++) {
      return props.cart[i].domainName === props.domainName;
    }
  };

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

0

Try the following code.

const addCart = () => {
  for (let i = 0; i < props.cart.length; i++) {
    if (props.cart[i].domainName === props.domainName) {
      return true;
    }
  }
  return false;
};

The function returns true when the first equal property has been found. It gives you the possibility to obtain the result without checking all elements of the array.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use some here

const addCart = () => {
  if (props.cart.some((o) => o.domainName === props.domainName)) return true;
  return false;
};

or simply using single liner solution

const addCart = () => props.cart.some((o) => o.domainName === props.domainName);
about 4 years ago · Juan Pablo Isaza Denunciar

0

I'd suggest:

const addCart = () => {
  // here we use Array.prototype.some(),
  // which returns a Boolean; true if any
  // of the array-elements match the provided
  // assessment, or false if not:
  return props.cart.some(
    (cart) => cart.domainName === props.domainName
  );
}

References:

  • Arrow functions.
  • Array.prototype.some().
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