I am facing an issue while trying to determine if a property exists in my object or not. If it exists, I want to increase a number associated with it; if it doesn't, I want to create the property and set the value to 1. I have tried using optional chaining and logical assignment, but encountered the following error: Uncaught SyntaxError: Invalid left-hand side expression in postfix operation Could you please help me identify the problem? Here is the code snippet:
const scores = {};
for (const player of Object.values(game.scored)) {
scores?.[player]++ && scores[player] = 1;
}
For reference, the variable game.scored contains 4 names.
I managed to achieve the desired outcome using a ternary operator, but I am curious as to why it did not work with optional chaining and logical assignment.