5 ms·
What is broken about for in? A quick search didn't turn up anything for me. I don't write much Javascript but for the times I do this seems like it could be use
by rascul 4y ago
What is broken about for in? A quick search didn't turn up anything for me. I don't write much Javascript but for the times I do this seems like it could be useful information for me.
- layer8 4y agoYou need to up your Google-foo. E.g.: https://stackoverflow.com/questions/500504/why-is-using-for-in-for-array-iteration-a-bad-idea https://stackoverflow.com/questions/500504/why-is-using-for-...
- pavlov 4y agoThat talks about using for…in with arrays, but not plain objects. IMO it’s fine for iterating over object keys. Sure, there are bizarre ways to make your objects go nuts in JavaScript through prototypal inheritance, but if you’re doing that then for…in is not your biggest problem.
- IshKebab 4y agoYou shouldn't normally need to iterate over object keys. Doing so is a pretty big indication of bad design. If you do you should use `Object.entries()` which avoids enumerating properties in the prototype chain. There's really no reason at all to use for-in and its use is usually a mistake.