简体   繁体   English

优化 if/else 语句

[英]Optimize if/else statement

I wrote the following code to simulate and simplify what is happening in my application.我编写了以下代码来模拟和简化我的应用程序中发生的事情。

In this simplification, I have the if and else branch that are executing the same code在这个简化中,我有执行相同代码的 if 和 else 分支

async function manipulDom(e) {
  const URItwo = `https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json`;
  let response = await fetch(URItwo);
  var data = await response.json();
  console.log(data);
  for (var key in data) {
    if (data[key].cc == "USD") {
      rateone.innerHTML =
        data[key].txt + ` ` + data[key].rate.toFixed(2) + `грн`;
      cursUSD = data[key].rate.toFixed(2);
      console.log(cursUSD);
    } else if (data[key].cc == "EUR") {
      ratetwo.innerHTML =
        data[key].txt + ` ` + data[key].rate.toFixed(2) + `грн`;
      cursEUR = data[key].rate.toFixed(2);
      console.log(cursEUR);
    } else if (data[key].cc == "PLN") {
      ratetree.innerHTML =
        data[key].txt + ` ` + data[key].rate.toFixed(2) + `грн`;
      cursPLN = data[key].rate.toFixed(2);
      console.log(cursPLN);
      return;
    }
  }
}

Use objects indexed by the currency abbreviations instead:改为使用按货币缩写索引的对象:

const cursObj = {
  USD: <value of cursUSD>,
  EUR: <value of cursEUR>,
  PLN: <value of cursPLN>
};
const rateElementsByCurrency = {
  USD: rateone,
  EUR: rateTwo,
  PLN: rateThree
}

async function manipulDom(e) {
  const URItwo = `https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json`;
  const response = await fetch(URItwo);
  const data = await response.json();
  data.forEach((obj) => {
    const { cc } = obj;
    if (cursObj[cc]) {
      cursObj[cc] = obj.rate.toFixed(2);
      rateElementsByCurrency[cc].textContent = obj.txt + ` ` + cursObj[cc] + `грн`;
      // If you're familiar with template literals, then you can do instead:
      // rateElementsByCurrency[cc].textContent = `${obj.txt} ${cursObj[cc]}грн`;
    }
  });
}

Because data is an array , not an object, iterate over it with forEach - don't use ` for..in to iterate over arrays.因为data是一个数组,而不是 object,所以使用forEach对其进行迭代 - 不要使用for..in对 arrays 进行迭代。

Because you want to put text into the rateElementsByCurrency elements, rather than HTML, you should use textContent instead of innerHTML , it's safer, quicker, and more predictable.因为您想将文本放入rateElementsByCurrency元素,而不是 HTML,所以您应该使用textContent而不是innerHTML ,这样更安全、更快捷、更可预测。

Try This尝试这个

 async function manipulDom(e) { const URItwo = `https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json`; const response = await fetch(URItwo); const data = await response.json(); var cc = ["USD", "EUR", "PLN"]; var exchangeRates = {}; for(value of data){ if(cc.includes(value.cc)){ let index = cc.indexOf(value.cc); cc.splice(index,index+1); value.rate = Number(value.rate).toFixed(2); exchangeRates[value.cc] = value; } if(cc.length < 1){break;} } console.log(exchangeRates); } manipulDom();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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