Javascript localstorage:localstorage中除一个键外的getAllItems
我正在修改一个HTML5应用程序,它记录了在项目上花费的时间、日期和项目代码。它使用localstorage,localstorage中有两种键—时间戳键和另一种称为“userdetails”的键。下面的代码列出了存储器中的所有数据。我只想列出带时间戳的数据,而不想列出键“userdetails”中包含的数据Javascript localstorage:localstorage中除一个键外的getAllItems,javascript,html,local-storage,Javascript,Html,Local Storage,我正在修改一个HTML5应用程序,它记录了在项目上花费的时间、日期和项目代码。它使用localstorage,localstorage中有两种键—时间戳键和另一种称为“userdetails”的键。下面的代码列出了存储器中的所有数据。我只想列出带时间戳的数据,而不想列出键“userdetails”中包含的数据 function getAllItems() { var timeLog = ""; var i = 0; var logLength = localStorage.length-1; v
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];
函数getAllItems(){
var timeLog=“”;
var i=0;
var logLength=localStorage.length-1;
var totalHours=0.0;
对于(i=0;i
函数getAllItems(){
var timeLog=“”;
var i=0;
var logLength=localStorage.length-1;
var totalHours=0.0;
对于(i=0;i这将吐出除用户详细信息之外的所有键:
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
函数getAllItems(){
//内务管理
var timeLog=“”
,i=0
,logLength=localStorage.length-1
,总小时数=0.0
,keytoignore='userDetails'
;
//循环遍历所有键
for(i=0;i getAllItems();
1382614467062 => 100 7 10/01/2013
1382614476654 => 200 3 10/02/2013
1382703706453 => 123 6 10/16/2013
总时数:16
谢谢Vicky。问题是我要列出的所有键都像“1382614467062”或“1382614476654”。没有名为“timestamp”的键,有一个键我不想包含“userdetails”。是否可以更改“if(itemKey==”timestamped“){”,以确保“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