0

I can not loop throught object in react with typescript. I am using redux toolkit and I want to get chosenFilters arrays.

const chosenFilters = useSelector((state: RootState) => state.recipe.filters);
  for (const chosenFilterArray of chosenFilters) {
    console.log(chosenFilterArray);
  }


error: 

Type 'filters' is not an array type or a string type.


interface: 

export interface filters {
  filterTypes: string[];
  filterLengths: string[];
}
1
  • Because filters isn't an array. It's an object with two properties which are arrays. Are you trying to loop over one or both of those properties? Loop over the properties of the object? Something else? Commented Mar 25, 2022 at 17:17

1 Answer 1

1

You can't iterate over an object with a for/of. But you can use Object.valus(someObj) to get the values from that object as an array and iterate over that.

for (const chosenFilterArray of Object.values(chosenFilters)) {
  console.log(chosenFilterArray);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.