0

Trying to check if an array has some values and display it once in typescript

const items = products?.reduce((prev: any, current) => {
  if (!current?.data()?.drinkName in prev) {
    prev[current?.data()?.category] = [];
  }
   prev[current?.data()?.category].push(current?.data()?.category);
   return prev;
}, {});

Error:

The left-hand side of an 'in' expression must be a private identifier or of type 'any', 'string', 'number', or 'symbol'.

1 Answer 1

2

You accidentally converted current?.data()?.drinkName to a boolean by adding ! before it. Try wrapping the whole statement with parenthesis like so !(current?.data()?.drinkName in prev). This way the ! negates the whole statement and not just the left part.

I would have also added a condition that checks whether current?.data()?.drinkName is null before checking if it exists in prev, because theoretically prev might contain null.

if (!(current?.data()?.drinkName) || !(current?.data()?.drinkName in prev))
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.