arrays - Retrieving multiple string values from localstorage in javascript -
i attempting pull string of data localstorage in javascript , combine multiple values multiple strings. example may have following 2 strings
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;
i want ignore first few , add last 5 or 6 each, value [13] in string + value [13] in string b = x, there doens of strings , have random numbers, have attached javascript code below , hoping can point me in right direction.
windows.onload= getallitems(); function getallitems(){ var = []; for(var = 0, j = localstorage.length; < j; i++) { all[i] = localstorage[i].split(';'); } var sum = 0; for(var = 0, j = all.length; < 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 />'); }
also worth mentioning assignment , such not allowed use json or jquery in capacity
edit: have changed code match answer given below, , think have taken output out of loop, incredibly new @ lot of hit , miss. doesnt seem working though looks should (to me)
edit2: adding javascript localstorage info source
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:
adding entire html page location of report including 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=[]; (i = 0,j=localstorage.length; i<j; i++) { all[i] = localstorage.getitem(localstorage.key(i)).split(';'); } function gettotal(index) { var sum = 0; for(var = 0, j = all.length; < 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>
your solution uses loop includes process - output! need separate process in two. loop part pick data , add up, output part doesn't need loop.
try this:
function getallitems(){ var all=[]; (i = 0,j=localstorage.length; i<j; i++) { all[i] = localstorage.getitem(localstorage.key(i)).split(';'); } function gettotal(index) { var sum = 0; for(var = 0, j = all.length; < 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 />'); }
Comments
Post a Comment