She
Posts: 0
Joined: Wed Oct 08, 2014 12:46 am

get count

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

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

get count

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

She
Posts: 0
Joined: Wed Oct 08, 2014 12:46 am

get count

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

Illya Stepanov
Posts: 0
Joined: Mon Mar 18, 2013 8:48 am

get count

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

She
Posts: 0
Joined: Wed Oct 08, 2014 12:46 am

get count

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

She
Posts: 0
Joined: Wed Oct 08, 2014 12:46 am

get count

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){

}

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

get count

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.

Return to “Issues”