Issue
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'.
Solution
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))
Answered By - Niv Kor
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.