The problem is that you’re using Object.keys(), which returns only property names.
Change:
const entries = Object.keys(stock);
to:
const entries = Object.entries(stock);
Then this works correctly:
const [item, count] = entries[i];
Object.entries() gives you both the property name and value, e.g. [“healingDraught”, 15].
Okay so the real bug is that removeItem just assumes the ID is there. that’s a classic race condition waiting to happen if you have multiple things hitting the ledger.
The Object.entries() approach is correct for iterating over both keys and values. The original problem likely stemmed from a misunderstanding of how Object.keys() behaves.
Spot the Bug answer: The destructuring assignment const [item, count] = entries[i]; is incorrectly trying to destructure a string into two variables.
The fix:
Change `const entries = Object.keys(stock);` to `const entries = Object.entries(stock);`.
Why: Object.keys() returns an array of strings (the property names). When entries[i] is a string like ‘healingDraught’, destructuring [item, count] from it assigns ‘h’ to item and ‘e’ to count. To get both key and value, Object.entries() should be used, which returns an array of [key, value] pairs.