Although I’m surprised there is no mention of the best solution for iterating both keys and values:
for (const [key, val] of Object.entries(obj)) { console.log(key, val) }
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Although I’m surprised there is no mention of the best solution for iterating both keys and values:
for (const [key, val] of Object.entries(obj)) { console.log(key, val) }
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
https://news.ycombinator.com/item?id=36458300
And according to the article, the issue isn't that TS doesn't realize it's the same object, it's that the object might have more keys than what is declared in your interface.
Perhaps what you meant to say is that you can directly use `val` instead of `obj[key]`.
The main issue at hand was inability to use `options[key]`.
"the object might have more keys" was a possible issue, and was the reason typescript was blocking access to `options[key]`, but it wasn't the main problem.
> It has that same problem actually:
> Perhaps what you meant to say is that you can directly use `val` instead of `obj[key]`.
How is that different from what I said?
Because you can directly use `val`, and because that's better than `obj[key]` anyway, you "don't have the problem of being unable to read obj[key]".
If I need to count occurrences of each character in a string, I'm using a loop.
Edit: ah I assume you mean more like a histogram. Yeah, that's going to be less clean, e.g.
input.split('').reduce((acc, el) => ({...acc, [el]: ( acc[el] ?? 0 ) + 1 }), {});