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

425
Vistas
How to write functions when type of values are different?

I have a problem with making my function smarter. I achieve good results, but I think about the other solution.

I can take boolean or object as parameter. When the parameter is the object I'd like to keep it as now. When the parameter is a boolean value, I'd like to create an object, where keys are taken from another variable (less important) and values are this boolean.

My solution assumes

  • adding an empty items object at the beginning
  • reading what is a value type and setting items according to the conditions
  • taking items and converting to an array
  • saving the array in state

Is that OK?

Check my code

const onAddAll = value => {
        let items = {}

        if (typeof value === 'boolean') {
            terms.forEach(term => items[ term ] = value)
        }

        if (typeof value === 'object') {
            items = value
        }

        const checked = Object.keys(items).filter(k => items[ k ])
        setChecked(checked)
    }

Imo, I wouldn't need the second condition and make it smarter, bot no idea how. Please help!

I use it here

<Checkbox
  items={getItems()}
  onChange={onAddAll}
>
  <label className="sr-only">
    {i18n.t('DROPDOWN.SELECT_ALL')}
  </label>
</Checkbox>

and here

<Dropdown>
  {[ 'all', 'none' ].map((key, i) => (
    <li key={key}
      className="dropdown__item">
      <div className="dropdown__link"
        onClick={onAddAll.bind(this, !i)}>                               
        {i18n.t(`DROPDOWN.${key.toUpperCase()}`)}
      </div>
    </li>
  ))}
</Dropdown>
about 4 years ago · Juan Pablo Isaza
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