question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: `proper-object-iterable-methods`

See original GitHub issue

Fail

// Should use `Object.values`
for (const key of Object.keys(foo)) {
	bar(foo[key]);
}
// Should use `Object.entries`
for (const key of Object.keys(foo)) {
	bar(foo[key], key);
}
// Should use `Object.values`
const foo = Object.keys(bar).map(key => baz(bar[key]));
// Should use `Object.entries`
const foo = Object.keys(bar).map(key => baz(bar[key], key));
// Should use `Object.keys`
for (const [key] of Object.entries(foo)) {
	bar(key);
}

Pass

for (const key of Object.keys(foo)) {
	foo[key] = 'new value';
	delete foo[key];
	foo[key]++;
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:11
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
fiskercommented, Jan 25, 2021

I think we should also enforce Object.keys over Object.entries in this case

for (const [key] of Object.entries(foo)) {
	bar(key);
}

So better come up a better name cover all them.

2reactions
fiskercommented, Jan 25, 2021

proper-object-iterable-methods?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ECMAScript proposal: iterator helpers - 2ality
In this blog post, we look at the ECMAScript proposal “Iterator helpers” by Gus ... This object has a method whose key is...
Read more >
Rule proposal: prevent array, iterable and function spreads ...
es2015.iterable.d.ts . The converse rule of preventing objects being spread within an array seems unnecessary/out-of-scope, given that ...
Read more >
Iteration protocols - JavaScript - MDN Web Docs
The iterator protocol defines a standard way to produce a sequence of values (either finite or infinite), and potentially a return value when ......
Read more >
PEP 234 – Iterators - Python Enhancement Proposals
Looping is customized by providing a method that produces an iterator object. The iterator provides a get next value operation that produces ...
Read more >
The Iterator Pattern - Python Design Patterns
The container must offer an __iter__() method that returns an iterator object. Supporting this method makes a container an iterable. Each iterator must...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found