Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

414
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!