Skip to content Skip to sidebar Skip to footer

Retrieving Last Key In Javascript Localstorage

I am doing a uni assignment so please excuse the coplete lack of knowledge here. I have been tasked with creating a web based app for a 'client' that requires me to open an invoice

Solution 1:

To display the last value of an array you should use values[values.length-1].

This will guarantee you get the absolute last one.


Solution 2:

simple put

var itemKey = localStorage.key(i);
var values = localStorage.getItem(itemKey); in a loop and increement i each time.

Solution 3:

You can try something like this

var localStorageKeys = Object.keys(localStorage);

getOrder(localStoragesKeys[localStoragesKeys.length-1]);

function getOrder(itemKey) {
    var values = localStorage.getItem(itemKey);
    values = values.split(";");
    var name = values[0];
    var company = values[1];
    var contactnumber = values[2];
    var email = values[3];
    var address1 = values[4];
    var address2 = values[5];
    var suburb = values[6]
    var postcode = values[7];
    var comments = values[8];
    var bags = values[9];
    var distance = values[10];
    var hdelivery_fee = values[11];
    var hprice = values[12];
    var htotal_notax = values[13];
    var hgst = values[14];
    var htotal_tax = values[15];
    var hordernumber  = values[16];


    document.write('Name: ' + name + '<br />');
    document.write('Company: ' + company + '<br />');
    document.write('Contact: ' + contactnumber + '<br />');
    document.write('Email; ' + email + '<br />');
    document.write('Address; ' + address1 + '<br />');
    document.write('Address; ' + address2 + '<br />');
    document.write('Suburb; ' + suburb + '<br />');
    document.write('Postcode; ' + postcode + '<br />');
    document.write('Comments; ' + comments + '<br />');
    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 />');
}

Solution 4:

If you must use localStorage and cannot use any client side database then I would save each entry with a key like new Date().getTime() with a known string in front of it, so that you have key-1368910344400.

When you need to retrieve the last entry then you could do:

var keys = Object.keys(localStorage);
keys = keys.map(function(key) {
  // check if the current key matches match "key-"
  // if that's the case create a new Date object with the ms obtained by a split
  // return the Date
});

// sort keys and take the most recent

This is usually a bad choice but as I understand it you're limited with choices, so you have to stretch things a bit.

If you could use a browser that supports html5 and webdb then just throw localStorage away and go for that :D, here is an example: http://www.html5rocks.com/en/tutorials/webdatabase/todo/


Post a Comment for "Retrieving Last Key In Javascript Localstorage"