简体   繁体   English

Javascript:遍历数组

[英]Javascript: loop through array

This is driving me crazy. 这真让我抓狂。 I'm just trying to print out an array and it's not working. 我只是想打印出一个数组,它不起作用。 What am I missing? 我想念什么? The results variable is returning "undefined" which much mean my for loop isn't working correctly. 结果变量返回“未定义”,这在很大程度上意味着我的for循环无法正常工作。 Everything else works properly, the console.log I have correctly displays the fields are added to the array. 其他所有内容均正常运行,我已经正确显示的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; } 

Here's a link to all the files. 这是所有文件的链接。 It's an iOS web app using Framework7. 这是一个使用Framework7的iOS Web应用程序。 Balance Pro 天平Pro

You are calling accountsListHtml() in body.onload . 您正在body.onload中调用body.onload accountsListHtml() At that point accountsArray is empty. 此时, accountsArray为空。 I can't find any other possibility to call accountsListHtml() on that page you linked to. 我找不到在您链接到的页面上调用accountsListHtml()任何其他可能性。

Add one line inside function addAccount() and it will work: 在函数addAccount()添加一行,它将起作用:

function addAccount() {

    /* vour code */

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

    accountsListHtml(); // add this line
}

Try changing results = accountsArray[i]; 尝试更改results = accountsArray[i]; to results += accountsArray[i]; results += accountsArray[i]; .

Update And initialize results with an empty string, for example :) 用空字符串更新和初始化results ,例如:)

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

The statement in the for loop ie results = accountsArray[i]; for循环中的语句,即results = accountsArray[i]; overwrites the variable results evry loop run. 覆盖每次循环运行时的变量results You could change the statement to : 您可以将语句更改为:

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

and initialise results to an empty string. 并将results初始化为空字符串。

The following works for me: http://jsfiddle.net/95ztrmk3/13/ 以下对我有用http : //jsfiddle.net/95ztrmk3/13/

HTML: HTML:

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

JS: 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);

}

Assuming the input isn't malformed or otherwise weird. 假设输入没有格式错误或其他异常。 I made sure Javascript recognizes results is an empty array and not a string or something: var results = [] 我确保Javascript可以识别results为空数组,而不是字符串或其他东西: var results = []

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

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