简体   繁体   English

如何避免在 javascript for 循环中重复代码?

[英]How do I avoid repeating code in a javascript for loop?

I have made a form with two dropdown menus:我制作了一个带有两个下拉菜单的表单:

 <select id="selectAccountCurrency"></select>

and

 <select name="Valutatype" id="selectCurrencyType"></select>

The dropdown menus are populated with various currencies using this function:使用此功能,下拉菜单中填充了各种货币:

function loadAllCurrencyKeys() {
  var keys = Object.keys(ECurrencyTypes);
  for (var index = 0; index < keys.length; index++) {
      var currencyKey = keys[index];
      var newOption = document.createElement("option");
      newOption.value = currencyKey;
      newOption.text = ECurrencyTypes[currencyKey].name;
      selectCurrencyType.options.add(newOption);
  }

for (var index = 0; index < keys.length; index++) {
      var currencyKey = keys[index];
      var newOption = document.createElement("option");
      newOption.value = currencyKey;
      newOption.text = ECurrencyTypes[currencyKey].name;
      selectAccountCurrency.options.add(newOption);
  }
}

Is there a way to make the second for loop shorter?有没有办法让第二个 for 循环更短? It seems to be a lot of repetition going on there.那里似乎有很多重复。

Additional information: Here are the object I'm trying to list in both dropdown menus at the same time:附加信息:这是我试图同时在两个下拉菜单中列出的对象:

  var ECurrencyTypes = {
NOK: {value:1.00000, name: "Norske kroner", denomination: "kr"},
EUR: {value:0.10733, name: "Europeiske euro", denomination: "€"},
USD: {value:0.12652, name: "United States dollar", denomination: "$"},
GBP: {value:0.09550, name: "Pound sterling", denomination: "£"},
INR: {value:8.18624, name: "Indiske rupee", denomination: "र"},
AUD: {value:0.16129, name: "Australienske dollar", denomination: "A$"},
PHP: {value:6.48595, name: "Filippinske peso", denomination: "₱"},
SEK: {value:1.02580, name: "Svenske kroner", denomination: "kr"},
CAD: {value:0.15841, name: "Canadiske dollar", denomination: "C$"},
THB: {value:4.18410, name: "Thai baht", denomination: "฿"}
};

And this is some of the html-code with the fieldsets and select-tags:这是一些带有字段集和选择标签的 html 代码:

<fieldset>
    <label for="txtAmount">Amount: </label>
    <input type="text" id="txtAmount">
    <label for="selectCurrencyType"> in </label>
    <select name="Valutatype" id="selectCurrencyType"></select>
</fieldset>

and

<fieldset>
    <label for="selectAccountCurrency">Currency:</label>
    <select id="selectAccountCurrency"></select>
</fieldset>

You have the second loop just becase you want to add the same option to both the select , but they need to be two different HTML elements.您有第二个循环只是因为您想向两个select添加相同的选项,但它们需要是两个不同的 HTML 元素。

Use just one loop, clone the element, and then append one to the first select and the cloned one to the other select, so you have just one loop, and they will be two different HTML elements, rather than the same one:只使用一个循环,克隆元素,然后将一个附加到第一个选择并将克隆的一个附加到另一个选择,因此您只有一个循环,它们将是两个不同的 HTML 元素,而不是同一个:

function loadAllCurrencyKeys() {
  var keys = Object.keys(ECurrencyTypes);
  for (var index = 0; index < keys.length; index++) {
      var currencyKey = keys[index];
      var newOption = document.createElement("option");
      newOption.value = currencyKey;
      newOption.text = ECurrencyTypes[currencyKey].name;
      selectCurrencyType.options.add(newOption);
      selectAccountCurrency.options.add(newOption.clone(true)); // <- see the .clone
  }
}

All the other answers seem to be correct, but you can make your code a bit less verbose by using Object.entries() and destructuring syntax in a for...of loop:所有其他答案似乎都是正确的,但是您可以通过在for...of循环中使用Object.entries()解构语法使代码不那么冗长:

function loadAllCurrencyKeys() {
  for (const [key, value] of Object.entries(ECurrencyTypes)) {
    const newOption = document.createElement("option");
    newOption.value = key;
    newOption.text = value;
    selectCurrencyType.options.add(newOption);
    selectAccountCurrency.options.add(newOption.clone(true));
  }
}

And if you have more than two <select> elements, you can iterate those as well by passing them to the function:如果你有两个以上的<select>元素,你也可以通过将它们传递给函数来迭代它们:

function loadAllCurrencyKeys(...selects) {
  for (const [key, value] of Object.entries(ECurrencyTypes)) {
    let newOption = document.createElement("option");
    newOption.value = key;
    newOption.text = value;

    for (const select of selects) {
      select.options.add(newOption);
      newOption = newOption.clone(true);
    }
  }
}

loadAllCurrencyKeys(selectCurrencyType, selectAccountCurrency);

The only downside being that the <option> is cloned one more time than necessary for each currency type, though that will most likely be trivial overhead.唯一的缺点是<option>比每种货币类型所需的多克隆一次,尽管这很可能是微不足道的开销。

You can just use a single for loop and then clone the newOption that was created before inserting it into the 2nd dropdown.您可以只使用一个 for 循环,然后clone在将其插入第二个下拉列表之前创建的newOption

 function loadAllCurrencyKeys() { var keys = Object.keys(ECurrencyTypes); for (var index = 0; index < keys.length; index++) { var currencyKey = keys[index]; var newOption = document.createElement("option"); newOption.value = currencyKey; newOption.text = ECurrencyTypes[currencyKey].name; selectCurrencyType.options.add(newOption); var clonedNewOption = newOption.clone(true); selectAccountCurrency.options.add(clonedNewOption); } }

If the dropdown values are different;如果下拉值不同;

function loadAllCurrencyKeys(data, select) {
      var keys = Object.keys(data);
      for (var index = 0; index < keys.length; index++) {
          var currencyKey = keys[index];
          var newOption = document.createElement("option");
          newOption.value = currencyKey;
          newOption.text = ECurrencyTypes[currencyKey].name;
          select.options.add(newOption);
      }
    }

and elsewhere in the code;以及代码中的其他地方;

loadAllCurrencyKeys(ECurrencyTypes, selectCurrencyType);
loadAllCurrencyKeys(ECurrencyTypes, selectAccountCurrency);

And if the dropdown values are the same for both the dropdowns, then you may hardcode the dropdown elements in the for loop.如果两个下拉列表的下拉值相同,那么您可以在 for 循环中对下拉元素进行硬编码。 ie IE

selectCurrencyType.options.add(newOption);
selectAccountCurrency.options.add(newOption);

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

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