Javascript:循环遍历数组

时间:2014-10-12 18:24:57

标签: javascript arrays

这让我发疯了。我只是想打印出一个阵列,但它无法正常工作。我错过了什么?结果变量返回“undefined”,这意味着我的for循环无法正常工作。其他一切正常,console.log我已正确显示字段已添加到数组中。

// The list of accounts array.
var accountsArray = [];

function addAccount() {
  // Take fields and put user data into varables.
  var accountName = document.getElementById('accountName').value;
  var accountBalance = document.getElementById('accountBalance').value;
  var accountType = document.getElementById("accountType");
  var accountTypeSelected = accountType.options[accountType.selectedIndex].text;
  var accountCurrency = document.getElementById("accountCurrency");
  var accountCurrencySelected = accountCurrency.options[accountCurrency.selectedIndex].text;

  // Put these variables into the array.
  accountsArray.push(accountName);
  accountsArray.push(accountBalance);
  accountsArray.push(accountTypeSelected);
  accountsArray.push(accountCurrencySelected);

  // Items added to the array, logged.
  console.log('user added: ' + accountsArray);
}

function accountsListHtml() {

  var results;

  // Loop through the array
  for (var i = 0; i < accountsArray.length; i++) {
    results = accountsArray[i];
  }

  document.getElementById('accountsList').innerHTML = results;
}

这是所有文件的链接。这是一个使用Framework7的iOS网络应用程序。 Balance Pro

4 个答案:

答案 0 :(得分:2)

您正在accountsListHtml()中呼叫body.onload。那时accountsArray是空的。 我找不到任何其他可能在您链接到的页面上调用accountsListHtml()

在函数addAccount()中添加一行,它将起作用:

function addAccount() {

    /* vour code */

    console.log('user added: ' + accountsArray);

    accountsListHtml(); // add this line
}

答案 1 :(得分:0)

尝试将results = accountsArray[i];更改为results += accountsArray[i];

<强>更新 并使用空字符串初始化results,例如:)

答案 2 :(得分:0)

for (var i = 0; i < accountsArray.length; i++) { results = accountsArray[i]; }

for循环中的语句,即results = accountsArray[i];将覆盖变量results evry循环运行。您可以将语句更改为:

results += accountsArray[i].toString();

并将results初始化为空字符串。

答案 3 :(得分:0)

以下内容适用于我:http://jsfiddle.net/95ztrmk3/13/

<强> HTML:

<div id="accountsList"></div>

<强> JS:

// The list of accounts array.
var accountsArray = [];

addAccount();
accountsListHtml();

function addAccount() {
    // Take fields and put user data into varables.
    var accountName = "John Doe";
    var accountBalance = "500.00";
    var accountTypeSelected = "Checking"
    var accountCurrencySelected = "USD";

    // Put these variables into the array.
    accountsArray.push(accountName);
    accountsArray.push(accountBalance);
    accountsArray.push(accountTypeSelected);
    accountsArray.push(accountCurrencySelected);

    // Items added to the array, logged.
    console.log('user added: ' + accountsArray);
}

function accountsListHtml() {

    var results = [];

    // Loop through the array
    for (var i = 0; i < accountsArray.length; i++) {
        results += accountsArray[i] + " ";
    }

    document.getElementById('accountsList').innerHTML = results;
    console.log(results);

}

假设输入格式不正确或其他奇怪。我确保Javascript识别results是一个空数组而不是字符串或其他内容:var results = []