我正在修改一个HTML5应用程序,记录在项目,日期和项目代码上花费的时间。它使用localstorage,localstorage中有两种键 - 时间戳键和另一个名为“userdetails”的键。下面的代码列出了存储中的所有数据。我想仅列出带时间戳的数据,而不是列出'userdetails'键中包含的数据。
function getAllItems() {
var timeLog = "";
var i = 0;
var logLength = localStorage.length-1;
var totalHours = 0.0;
for (i = 0; i <= logLength; i++) {
var itemKey = localStorage.key(i);
var values = localStorage.getItem(itemKey);
values = values.split(";");
var code = values[0];
var hours = values[1];
var date = values[2];
答案 0 :(得分:1)
function getAllItems() {
var timeLog = "";
var i = 0;
var logLength = localStorage.length-1;
var totalHours = 0.0;
for (i = 0; i <= logLength; i++) {
var itemKey = localStorage.key(i);
if(itemKey == 'timestamped' ){
var values = localStorage.getItem(itemKey);
values = values.split(";");
var code = values[0];
var hours = values[1];
var date = values[2];
}
}
}
function getAllItems() {
var timeLog = "";
var i = 0;
var logLength = localStorage.length-1;
var totalHours = 0.0;
for (i = 0; i <= logLength; i++) {
var itemKey = localStorage.key(i);
if(itemKey == 'timestamped' ){
var values = localStorage.getItem(itemKey);
values = values.split(";");
var code = values[0];
var hours = values[1];
var date = values[2];
}
}
}
答案 1 :(得分:0)
这会吐出所有键,但 userDetails :
function getAllItems() {
// housekeeping
var timeLog = ""
, i = 0
, logLength = localStorage.length-1
, totalHours = 0.0
, theKeyToIgnore = 'userDetails'
;
// loop through all keys
for( i = 0; i <= logLength; i++ ) {
var itemKey = localStorage.key( i )
, values = localStorage.getItem( itemKey )
;
// is this the ignored key? then skip a loop iteration
if( itemKey === theKeyToIgnore ) { continue; }
values = values.split( ";" );
// set our data
var code = values[ 0 ]
, hours = values[ 1 ]
, date = values[ 2 ]
;
// display it
console.log( itemKey + ' => ' + code + ' ' + hours + ' ' + date );
// tally total hours
totalHours += parseFloat( hours );
}
// display total hours
console.log( 'total hours: ' + totalHours );
}
> getAllItems();
1382614467062 => 100 7 10/01/2013
1382614476654 => 200 3 10/02/2013
1382703706453 => 123 6 10/16/2013
total hours: 16