'Getting errors while trying some condition on an array
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 1:[1]
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))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Niv Kor |
