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

312
Views
How do I loop through the elements of an object and return its value in TypeScript&React?

I have the following enum and state:

enum FilterId {
    filter1,
    filter2,
    filter3,
    filter4,
    filter5,
}
type FiltersInComponent = { [key in FilterId]: boolean };

const [appliedFilters, setAppliedFilters] = React.useState<FiltersInComponent>()

Question 1: How do I initialise the appliedFilters state to (in short notation):

{ 
  filter1: false, 
  filter2: false, 
  filter3: false, 
  filter4: false, 
  filter5: false 
}

Question 2: How do I loop through appliedFilters and display a checkbox? I have the following so far:

{Object.keys(appliedFilters)
    .filter((v) => !isNaN(Number(v)))
    .map((option) => {
        const isChecked: boolean = appliedFilters[option];
        return (
            <>
                <Checkbox checked={isChecked} />
                <Typography>
                    {optionMessage(Number(option))}
                </Typography>
            </>
        );
     })}

But I'm getting the following error for appliedFilters[option]

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'FiltersInComponent'.
  No index signature with a parameter of type 'string' was found on type 'FiltersInComponent'.
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is in the way you've defined this enum.

You're looking for something more along these lines:

type FilterId = 'filter1' | 'filter2' | 'filter3' | 'filter4' | 'filter5'

const foo = () => {
  type FiltersInComponent = { [key in FilterId]: boolean }

  const [appliedFilters, setAppliedFilters] =
    React.useState<FiltersInComponent>({
      filter1: false,
      filter2: false,
      filter3: false,
      filter4: false,
      filter5: false,
    })

As you have it right now [key in FilterId] evaluates to

type FiltersInComponent = {
    0: boolean;
    1: boolean;
    2: boolean;
    3: boolean;
    4: boolean;
}

as your enum values are the defaults of 0,1,2 etc. You could if you wanted, add enum values of 'filter1' etc and then use a string template literal to extract type FilterIdType = ${FilterId}; But if you're going to modify your enum and have control over it you're better off just going for the simpler union type.

about 4 years ago · Juan Pablo Isaza Report
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!