检索以特定单词开头的 localStorage 值
Retrieve localStorage values which start with certain words
我正在存储一些 localStorage 项目如下:
var id = $(this).closest('.item').attr('data-id');
var title = $(this).closest('.item').attr('data-title');
var json ={"id":id,"title":title};
localStorage.setItem('clip ' +id, JSON.stringify(json));
给我…
Key ------------------------------ Value
clip 14014_life-ring ---------- {"id":"14014_life-ring","title":"Life
Ring"} clip 14015_life-jacket -------
{"id":"14015_life-jacket","title":"Life Jacket"}
要检索/显示特定值,我正在使用:
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
json = localStorage.getItem( localStorage.key( i ) );
var result = JSON.parse(json);
list.append("<li><input type=\"hidden\" name=\"id[]\" value=\"" + result['id'] + "\"><input type=\"hidden\" name=\"title[]\" value=\"" + result['title'] + "\"></li>”);
//console.log(result);
}
这很好用,但我还想将一些其他更简单的项目(非 JSON)存储为单独的键/值,例如输入字段值(通过检测 keyup 事件),即姓名、电子邮件地址等。
如何编写上面的 for 循环,以便仅显示以“clip”开头的键?
另外,我也想只统计'clip'开头的key,目前我只知道如何统计all个localStorage项的总量这会在存储其他项目时混淆事情......
$('.clip-total').html(localStorage.length);
有办法吗?
function showKeys(onlyClipKeys) {
var totalBeginningWithClip = 0;
for (var i = 0, len = localStorage.length; i < len; i++) {
var key = localStorage.key(i);
json = localStorage.getItem(key);
console.log(key);
var result = JSON.parse(json);
if (onlyClipKeys) {
if (key.indexOf('clip') === 0) {
totalBeginningWithClip++;
list.append("<li>" + result['id'] + " - " + result['title'] + "</li>");
}
} else {
list.append("<li>" + result['id'] + " - " + result['title'] + "</li>");
}
}
return totalBeginningWithClip;
}
我正在存储一些 localStorage 项目如下:
var id = $(this).closest('.item').attr('data-id');
var title = $(this).closest('.item').attr('data-title');
var json ={"id":id,"title":title};
localStorage.setItem('clip ' +id, JSON.stringify(json));
给我…
Key ------------------------------ Value
clip 14014_life-ring ---------- {"id":"14014_life-ring","title":"Life Ring"} clip 14015_life-jacket ------- {"id":"14015_life-jacket","title":"Life Jacket"}
要检索/显示特定值,我正在使用:
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
json = localStorage.getItem( localStorage.key( i ) );
var result = JSON.parse(json);
list.append("<li><input type=\"hidden\" name=\"id[]\" value=\"" + result['id'] + "\"><input type=\"hidden\" name=\"title[]\" value=\"" + result['title'] + "\"></li>”);
//console.log(result);
}
这很好用,但我还想将一些其他更简单的项目(非 JSON)存储为单独的键/值,例如输入字段值(通过检测 keyup 事件),即姓名、电子邮件地址等。
如何编写上面的 for 循环,以便仅显示以“clip”开头的键?
另外,我也想只统计'clip'开头的key,目前我只知道如何统计all个localStorage项的总量这会在存储其他项目时混淆事情......
$('.clip-total').html(localStorage.length);
有办法吗?
function showKeys(onlyClipKeys) {
var totalBeginningWithClip = 0;
for (var i = 0, len = localStorage.length; i < len; i++) {
var key = localStorage.key(i);
json = localStorage.getItem(key);
console.log(key);
var result = JSON.parse(json);
if (onlyClipKeys) {
if (key.indexOf('clip') === 0) {
totalBeginningWithClip++;
list.append("<li>" + result['id'] + " - " + result['title'] + "</li>");
}
} else {
list.append("<li>" + result['id'] + " - " + result['title'] + "</li>");
}
}
return totalBeginningWithClip;
}