[英]I want to remove item from local storage key with the function
function remove(current) {
let text = "Are You Sure ?";
if (confirm(text) == true) {
current.parentElement.parentElement.remove();
// document.getElementById("tablebody1").deleteRow(element.parentNode.rowIndex);
text33 = JSON.parse(localStorage.getItem("storex"))
function rem(arr, index) {
return arr.filter(function(ele) {
return ele != index
})
}
localStorage.setItem('text32', JSON.stringify(result));
localStorage.getItem("text32")
}
}
我正在嘗試從本地存儲中刪除項目我在屏幕上呈現表數據,但我的表數據正在進入本地存儲我想刪除特定項目,例如:
如果我有這樣的本地存儲
0: {namex: "ANUJ", lnamex: "ANUJ", emailx: "anuj123@gmail.com"}
1: {namex: "ANUJ", lnamex: "ANUJ", emailx: "anuj123@gmail.com"}
2: {namex: "ANUJ", lnamex: "ANUJ", emailx: "anuj123@gmail.com"}
3: {namex: "ANUJ", lnamex: "ANUJ", emailx: "anuj123@gmail.com"}
4: {namex: "ANUJ", lnamex: "ANUJ", emailx: "anuj123@gmail.com"}
5: {namex: "ANUJ", lnamex: "ANUJ", emailx: "anuj123@gmail.com"}
6: {namex: "rohit", lnamex: "ANUJ", emailx: "anuj123@gmail.com"}
假設我要刪除 6 個項目。 我該怎么做,或者如果我在刷新頁面時將其刪除,它會再次出現
您可以使用此方法刪除localstorage
項目:
//setting item localStorage.setItem('image', 'myCat.png'); //removing item localStorage.removeItem('image');
function example(){
let array = [
{namex: "1", lnamex: "ANUJ", emailx: "anuj123@gmail.com"},
{namex: "2", lnamex: "ANUJ", emailx: "anuj123@gmail.com"},
{namex: "3", lnamex: "ANUJ", emailx: "anuj123@gmail.com"},
{namex: "4", lnamex: "ANUJ", emailx: "anuj123@gmail.com"},
{namex: "5", lnamex: "ANUJ", emailx: "anuj123@gmail.com"},
{namex: "6", lnamex: "ANUJ", emailx: "anuj123@gmail.com"}
];
localStorage.setItem('text32', JSON.stringify(array));
let getDataFromLocalStorage = JSON.parse(localStorage.getItem("text32")); //convert your saved string array to object
let result = getDataFromLocalStorage.filter((x, idx) => idx != 1);
document.getElementById('ex').innerHTML = JSON.stringify(result)
}
您必須將數組保存為本地存儲中的字符串,因此您必須將其取回並將其轉換為 Object 並使用filter()方法。 根據上面的例子,它將給出除第一個索引對象之外的所有數據
如果要刪除第 6 個,則必須將索引號發送為 5,因為數組對象從第 0 個索引開始
然后如果需要,將其作為字符串保存回本地存儲
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.