Page 1 of 1

get count

Posted: Thu Mar 12, 2015 10:13 am
by She

Hi Team,
How to count JSON in localStorage and delete the first value when he reach the limit:

i got a code but its not working no error found.
example JSON:
[{"id":"","DeviceID":"","Timedata":"March 12,2015 4:27:50 PM","Longitude":"121.0437003","Latitude":"14.6760413"},{"id":"","DeviceID":"","Timedata":"March 12,2015 5:00:40 PM","Longitude":"121.0437003","Latitude":"14.6760413","ImageBlob":null},{"id":"","DeviceID":"","Timedata":"March 12,2015 5:00:40 PM","Longitude":"121.0437003","Latitude":"14.6760413","ImageBlob":null},{"id":"","DeviceID":"","Timedata":"March 12,2015 5:00:40 PM","Longitude":"121.0437003","Latitude":"14.6760413","ImageBlob":null},{"id":"","DeviceID":"","Timedata":"March 12,2015 5:00:40 PM","Longitude":"121.0437003","Latitude":"14.6760413","ImageBlob":null},{"id":"","DeviceID":"","Timedata":"March 12,2015 5:00:40 PM","Longitude":"121.0437003","Latitude":"14.6760413","ImageBlob":null},{"_id":"","DeviceID":"","Timedata":"March 12,2015 5:00:51 PM","Longitude":"121.0437003","Latitude":"14.6760413","ImageBlob":null}]

my code in pageshow is the ff:

var arr = localStorage.getItem("_TimeLogsStorage");
var x = arr.length;
if(x = 5){
x.remove(0);
}

thanks


get count

Posted: Thu Mar 12, 2015 2:14 pm
by Bruce Stuart

Is x = to 1 when you run this or what is the value of x ?


get count

Posted: Fri Mar 13, 2015 1:50 am
by She

i change the code like this:
var arr =localStorage.getItem("_TimeLogsStorage");
var count = jQuery.parseJSON(arr);
alert(count.length);// 7 total length
var total = count.length;

if (total 5){
arr.splice(0,1);
Appery("mobList").listview("refresh");
}

this is the error in console:
Uncaught TypeError: undefined is not a function.
why im getting this


get count

Posted: Fri Mar 13, 2015 10:58 am
by Illya Stepanov

Hi She - Could you please show us the exact error message screen shot?


get count

Posted: Mon Mar 16, 2015 12:08 am
by She

Hi Ilya
i just want to get the total records of JSON for the localStorage. and delete the first json records


get count

Posted: Mon Mar 16, 2015 12:52 am
by She

i already got this by
var js = JSON.parse(localStorage.getItem("_TimeLogsStorage")).length;
now how can i delete the first element of the localStorage

if (js 5){

}


get count

Posted: Tue Mar 17, 2015 4:17 am
by Yurii Orishchuk

Hi She,

Here is a work example.

pre

var arr = localStorage.getItem("_TimeLogsStorage");

//Make JS object from JSON string.
arr = JSON.parse(arr);

//Get length of the array
var x = arr.length;

//Compare length to some value...
if(x = 5){
//Remove first item from your array.
arr.splice(0, 1);
}

//store array back to LSV.
localStorage.setItem("_TimeLogsStorage", JSON.stringify(arr));

/pre

Note: there is some comments for this code. Please learn them.

Regards.