簡體   English   中英

React 中的 for 循環顯示 ESLint 錯誤

[英]The for loop in React showing ESLint error

objToQueryString 函數內的 for 循環向我顯示錯誤

ESLint:for-in 的主體應該包含在 if 語句中,以過濾原型中不需要的屬性。(guard-for-in)

const objToQueryString = obj => {
  const keyValuePairs = [];
  for (const key in obj) {
    keyValuePairs.push(
      `${encodeURIComponent(key)}${encodeURIComponent(
        `: "`
      )}${encodeURIComponent(obj[key])}`
    );
  }
  return `${encodeURIComponent(`{`)}${keyValuePairs.join(
    encodeURIComponent(`", `)
  )}${encodeURIComponent(`" }`)}`;
};

並添加: // eslint-disable-next-line guard-for-in不起作用

const objToQueryString = (obj) => {
  const keyValuePairs = [];
  for (const key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      keyValuePairs.push(
        `${encodeURIComponent(key)}${encodeURIComponent(
          ': "',
        )}${encodeURIComponent(obj[key])}`,
      );
    }
  }
  return `${encodeURIComponent('{')}${keyValuePairs.join(
    encodeURIComponent('", '),
  )}${encodeURIComponent('" }')}`;
};

更多關於規則的細節和解釋在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM