在javascript中从localstorage中检索多个字符串值

Retrieving multiple string values from localstorage in javascript

本文关键字:字符串 检索 javascript 中从 localstorage      更新时间:2023-09-26

我试图从javascript本地存储中拉出一串数据,并从多个字符串组合多个值。例如我可能有以下两个字符串

Bob;companyA;6141692120;email1@email.com;1 John St;;Bellevue;6056;;6;10;20.00;52.800000000000004;72.80;7.28;80.08;
Jane;companyB;6157692120;email2@email.com;1 Jack St;;Bellevue;6056;;6;10;20.00;52.800000000000004;72.80;7.28;80.08;

我想忽略前几个并从每个中添加最后5或6个,所以字符串A中的值[13]+字符串B中的值[13]= X,将有数十个字符串,它们都有随机数,我已经附上了下面的javascript代码,我希望有人能指出我在正确的方向。

windows.onload= getAllItems();
function getAllItems(){
var all = [];
for(var i = 0, j = localStorage.length; i < j; i++) {
    all[i] = localStorage[i].split(';');
}
var sum = 0;
for(var i = 0, j = all.length; i < j; i++) {
    sum += all[i][12];
}
var bags = all[9];
var distance = all[10];
var hdelivery_fee = all[11];
var hprice = all[12];
var htotal_notax = all[13];
var hgst = all[14];
var htotal_tax = all[15];
var hordernumber  = all[16];
document.write('Number of Bags; ' + bags + '<br />');
document.write('Distance; ' + distance + '<br />');
document.write('Delivery Fee; $' + hdelivery_fee + '<br />');
document.write('Price of Bags; $' + hprice + '<br />');
document.write('Total Ex-GST; $' + htotal_notax + '<br />');
document.write('GST; $' + hgst + '<br />');
document.write('Total Inc GST; $' + htotal_tax + '<br />');
document.write('hordernumber; ' + hordernumber + '<br />');
}

也可能值得一提的是,这是一个作业,因此我不允许在任何情况下使用JSON或jquery

编辑:我已经改变了代码,以匹配下面给出的答案,我认为我已经把输出从循环中取出,但是我在这方面是令人难以置信的新,所以很多都是命中和失误。它似乎并不像工作一样,即使它看起来像它应该(对我来说)

EDIT2:添加javascript为本地存储信息源

function source()
{
var newDate = new Date();
var itemId = newDate.getTime();
var values = new Array();
    var name = document.getElementById("name").value;
    var company = document.getElementById("company").value;
    var contactnumber= document.getElementById("contactnumber").value;
    var email = document.getElementById("email").value;
    var address1 = document.getElementById("address1").value;
    var address2 = document.getElementById("address2").value;
    var suburb = document.getElementById("suburb").value;
    var postcode = document.getElementById("postcode").value;
    var comments = document.getElementById("comments").value;
    var bags = document.getElementById("bags").value;
    var distance = document.getElementById("distance").value;
    var hdelivery_fee = document.getElementById("hdelivery_fee").value;
    var hprice = document.getElementById("hprice").value;
    var htotal_notax = document.getElementById("htotal_notax").value;
    var hgst = document.getElementById("hgst").value;
    var htotal_tax= document.getElementById("htotal_tax").value;
    var hordernumber= document.getElementById("hordernumber").value;

values.push(name);
values.push(company);
values.push(contactnumber);
values.push(email);
values.push(address1);
values.push(address2);
values.push(suburb);
values.push(postcode);
values.push(comments);
values.push(bags);
values.push(distance);
values.push(hdelivery_fee);
values.push(hprice);
values.push(htotal_notax);
values.push(hgst);
values.push(htotal_tax);
values.push(hordernumber);
try {
    localStorage.setItem(itemId, values.join(";")); 
} catch (e) {
    if (e == QUOTA_EXCEEDED_ERR) {
        alert("Quota exceeded!");
    }
}
}

EDIT3:

添加整个HTML页面用于报告的位置,包括javascript。

<html>
<head>
<title>Form Processor</title>
     <link rel="stylesheet" type="text/css" href="layout.css">


</head>
<body>
<div id="header">
        <h1 id="main_tile"> Everwarm Fuel Merchants - Daily Report </h1>
    </div>
<h1> Daily Sales Summary <h1>
<p>
<script>
function getAllItems(){
    var all=[];
    for (i = 0,j=localStorage.length; i<j; i++) {
        all[i] = localStorage.getItem(localStorage.key(i)).split(';');
    }
    function getTotal(index) {
         var sum = 0;
         for(var i = 0, j = all.length; i < j; i++) {
            sum += parseFloat(all[i][index]);
        }
        return sum;
    }
    var bags = getTotal(9);
    var distance = getTotal(10);
    var hdelivery_fee = getTotal(11);
    var hprice = getTotal(12);
    var htotal_notax = getTotal(13);
    var hgst = getTotal(14);
    var htotal_tax = getTotal(15);
    var hordernumber = getTotal(16);
    document.write('Number of Bags; ' + bags + '<br />');
    document.write('Distance; ' + distance + '<br />');
    document.write('Delivery Fee; $' + hdelivery_fee + '<br />');
    document.write('Price of Bags; $' + hprice + '<br />');
    document.write('Total Ex-GST; $' + htotal_notax + '<br />');
    document.write('GST; $' + hgst + '<br />');
    document.write('Total Inc GST; $' + htotal_tax + '<br />');
    document.write('hordernumber; ' + hordernumber + '<br />');
  }</script>
  </p>

<input type="button" value="Clear storage" onclick="localstorage.clear()" > </input>
<input type="button" value="Return" onclick="history.back()"> </input>
</body>
</html> 

您的解决方案使用了一个包含所有过程的循环-甚至包括输出!你需要把这个过程分成两部分。循环部分将拾取所有数据并将其全部相加,然后输出部分不需要循环。

试试这个:

function getAllItems(){
    var all=[];
    for (i = 0,j=localStorage.length; i<j; i++) {
        all[i] = localStorage.getItem(localStorage.key(i)).split(';');
    }
    function getTotal(index) {
         var sum = 0;
         for(var i = 0, j = all.length; i < j; i++) {
            sum += parseFloat(all[i][index]);
        }
        return sum;
    }
    var bags = getTotal(9);
    var distance = getTotal(10);
    var hdelivery_fee = getTotal(11);
    var hprice = getTotal(12);
    var htotal_notax = getTotal(13);
    var hgst = getTotal(14);
    var htotal_tax = getTotal(15);
    var hordernumber = getTotal(16);
    document.write('Number of Bags; ' + bags + '<br />');
    document.write('Distance; ' + distance + '<br />');
    document.write('Delivery Fee; $' + hdelivery_fee + '<br />');
    document.write('Price of Bags; $' + hprice + '<br />');
    document.write('Total Ex-GST; $' + htotal_notax + '<br />');
    document.write('GST; $' + hgst + '<br />');
    document.write('Total Inc GST; $' + htotal_tax + '<br />');
    document.write('hordernumber; ' + hordernumber + '<br />');
  }