简体   繁体   中英

Caught between two no-restricted-syntax violations

This is my original code:

const buildTableContent = (settings) => {
  const entries = [];
  for (const key in settings) {
    for (const subkey in env[key]) {

settings is basically a dictionary of dictionary

  {  
    'env': {'name': 'prod'}, 
    'sass: {'app-id': 'a123445', 'app-key': 'xxyyzz'}
  }

It triggered the following AirBnb style guide error:

35:3 error for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array no-restricted-syntax

So I change the code to

const buildTableContent = (settings) => {
  const entries = [];
  for (const key of Object.keys(settings)) {
    for (const subkey of Object.keys(env[key])) {

as suggested.

Now when I run lint , I got this:

35:3 error iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations no-restricted-syntax

So it looks to me either way they are violating some lint style.

How can I fix this issue?

You'd want to use

Object.keys(settings).forEach(key => {
  Object.keys(env[key]).forEach(subkey => {

or potentially Object.entries or Object.values depending on if you actually want the keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM