Javascript 在localStorage中存储阵列时出错

Javascript 在localStorage中存储阵列时出错,javascript,Javascript,我的代码中有一个bug,在重新加载时只保存数组中的最后一个对象。我感觉addAccount()函数没有正确保存或插入数据。其他一切都正常工作。在我的控制台中,它显示数据正在插入到数组中,但当我刷新时,我只保存最后一个对象。 我不知道该怎么办。 //帐户列表数组。 var accountsArray=[]; 函数addAccount(){ //获取字段并将用户数据放入变量中。 var accountName=document.getElementById('accountName')。值; var

我的代码中有一个bug,在重新加载时只保存数组中的最后一个对象。我感觉addAccount()函数没有正确保存或插入数据。其他一切都正常工作。在我的控制台中,它显示数据正在插入到数组中,但当我刷新时,我只保存最后一个对象。 我不知道该怎么办。
//帐户列表数组。
var accountsArray=[];
函数addAccount(){
//获取字段并将用户数据放入变量中。
var accountName=document.getElementById('accountName')。值;
var accountBalance=document.getElementById('accountBalance')。值;
var accountType=document.getElementById(“accountType”);
var accountTypeSelected=accountType.options[accountType.selectedIndex].text;
var accountCurrency=document.getElementById(“accountCurrency”);
var accountCurrencySelected=accountCurrency.options[accountCurrency.selectedIndex].text;
变量临时对象={
“accountName”:accountName,
“accountBalance”:accountBalance,
“accountTypeSelected”:accountTypeSelected,
“accountCurrencySelected”:accountCurrencySelected
};
accountsArray.push(临时对象);
console.log(accountsArray);
saveAccountData();
showcountsarray();
}
函数saveAccountData(){
setItem('accountsArray',JSON.stringify(accountsArray));
}
函数showcountsarray(){
//var accountsLocalStorage=JSON.parse(localStorage['accountsArray']);
if(localStorage.getItem(“accountsArray”)==null){
document.getElementById(“getStarted”).style.visibility=“visible”;
document.getElementById(“balanceToolbarName”).style.visibility=“hidden”;
document.getElementById(“accountsMainList”).style.visibility=“hidden”;
}否则{
var accountsLocalStorage=JSON.parse(localStorage['accountsArray']);
console.log(accountsLocalStorage);
var accountInfo='';
var i=0;
while(i