Solvedjavascript Using 'ForInStatement' is not allowed (no-restricted-syntax)

Since version 8, there is this error on for in loops.
What should be used in place of for-in loops ?

55 Answers

✔️Accepted Answer

@francoisromain Object.assign(options, opts)

Other Answers:

Use things like map/every/some/filter/reduce/find/findIndex etc to iterate over arrays, and Object.keys/Object.values/Object.entries to produce arrays so you can iterate over objects.

Feel free to provide some loops (ideally concrete use cases and not contrived examples), and I'll happily show you a better way to write the code with iteration instead of a loop.


const count = Object.values(this.state.selected).filter(Boolean).length;
/* or, if you really need to only check for literal true */
const count = Object.values(this.state.selected).filter(x => x === true).length;


my loop was like this:

for (const i in options) {
    if (opts && opts.hasOwnProperty(i) && options.hasOwnProperty(i)) {
        options[i] = opts[i];

and it is now like this:

Object.keys(options).forEach((key, index) => {
    if (opts && opts.hasOwnProperty(key)) {
        options[key] = opts[key];

is there a better way?

This thread is already 3 years old and you're still teaching and helping people out with their iterations. You're such a good sport, @ljharb.

More Issues: