【问题标题】:Saving Array to Chrome Local Storage将数组保存到 Chrome 本地存储
【发布时间】:2015-12-25 10:14:30
【问题描述】:

我正在将一个数组保存到本地存储,然后将数组中的单词添加到我正在编写的 chrome 扩展程序中使用的 popup.html 页面上的ul。

问题是在我离开 popup.html 后,我保存到本地存储的关键字不再出现在表单中。

这让我相信我没有将它们保存到本地存储中。

我是否正确保存数组并正确调用存储在本地存储中的数组中的关键字?

  keyWordArray = [];
keyWords = $('#keyWords').html();
keyWordArray = localStorage.keyWords;

keyIn = localStorage.setItem("keyWords", JSON.stringify(keyWordArray));
keyOut= JSON.parse(keyWordArray);

Description = $('#description').val();

loadKeys = function loadKeyWords() {
  //clear the existing data
  $('#keyWords').html('');
    //for all the items in the array...
    for(var i = 0; i < keyOut.length; i++) {
      //add them to the UL
      $('#keyWords').append('<li><input id="check" name="check" type="checkbox">'+keyWordArray[i]+'</li>'); 
        }
    };

$(document).ready(function () {

  $('#add').click( function() {

    if($(Description) === '') {
      $('#alert').html("<strong>Warning!</strong> Enter some things you hate!");
      $('#alert').fadeIn().delay(1000).fadeOut();
      return false;
    }

    //add the new keyword to the array
    keyWordArray.push(Description);
    //overwrite the array in localStorage with the new, updated array
    keyIn();
    //call loadKeyWords() to rebuild the UL and hide any matching elements
    loadKeys();
    return false;
  });

  $('#clear').click( function() {
    window.localStorage.clear();
    location.reload();
    return false;
  });

window.addEventListener('DOMContentLoaded', function() {
    var page = chrome.extension.getBackgroundPage();
    // now you can access the background page objects / functions
      for(var i = 0; i < keyWordArray.length; i++) {
      //and hide any DOM elements that have the keyword
      $("div:contains('"+keyWordArray[i]+"')").hide();    
        }
    });

loadKeys();
}); //end of document ready function

【问题讨论】:

  • 我相信 localstorage 只接受键值对,这可能是它不直接保存数组的原因。也许尝试使用localStorage.keyWords =keyWordArray 或localStorage.setItem("keyWords", keyWordArray)
  • 好吧,我认为你的权利从我读到的。 JS不是我最强的。您是说添加该行,它应该修复我的程序或更改我提到本地存储的任何地方?

标签: javascript arrays google-chrome-extension local-storage


【解决方案1】:

这是一个与您的问题无关的解决方案,它将数组显式保存到本地存储:

// set up the array
var theArray = ['red', 'green', 'blue'],
    localStorageTheArray;

// convert the array to a string and write it to local storage 
localStorage.setItem('theArray', JSON.stringify(theArray));

// retrieve the string from local storage and convert it back to an array
localStorageTheArray = JSON.parse(localStorage.getItem('theArray'));

localStorageTheArray.push('purple');

console.log(localStorageTheArray);
// ["red", "green", "blue", "purple"]

链接到JS Fiddle

函数示例

function setArrayInLocalStorage(key, array) {
    localStorage.setItem(key, JSON.stringify(array));
}

function getArrayInLocalStorage(key) {
    return JSON.parse(localStorage.getItem(key));
}

// to set
setArrayInLocalStorage('theArray', theArray);

// to get
getArrayInLocalStorage('theArray');

【讨论】:

  • 所以在这个例子中是 theArray 也是本地存储中的键。所以对我来说,如果我将键作为关键字,数组作为 myArray,那么 localStorage.setItem('keyword', JSON.stringify(myArray)); ?
  • 是的,正确的。然后,您将检索该密钥 localStorageTheArray = JSON.parse(localStorage.getItem('keyword'));
  • 所以在你最后 consolo.log 之后,我可以直接推送到数组并打印数组,而无需再次调用 stringifya 和解析吗?它是否只需要在创建后完成一次?还是我需要将其视为来回移动并且每次都需要转换的东西?
  • 您需要每次转换它。每次写入本地存储时,都需要进行字符串化,每次都需要解析。我编写了一个函数,每次都可以为您执行此操作。
【解决方案2】:

替换

localStorage["keyWords"] = JSON.stringify(keyWordArray);

与:

localStorage.setItem("keyWords", JSON.stringify(keyWordArray));

然后在访问关键字时使用:

localStorage.keyWords

代替:

localStorage["keyWords"]

不能保证能解决所有问题,但应该能解决本地存储问题。

【讨论】:

  • 当我更新我的代码时,它不会获取任何新值并显示它们,但它会将这些值添加到本地存储并显示它直到我删除它。 [“d e 2 d”、“d e 2 d”、“d e 2 d”、“d e 2 d”]
  • 如果您不介意检查我,我更新了我的代码。也感谢您的帮助。
  • “它不会采用任何新值并显示它们”是什么意思
  • 当我尝试向数组添加一个新关键字时,它不会显示它,它只显示 de2d 的东西。我正在将要添加到本地存储的关键字列表打印到 ul 中的屏幕上。
  • 我更改了代码,所以我更新了它。我在全局范围内创建了您给我的陈述。现在我可以将新关键字添加到本地存储,它会在添加的屏幕上显示它们,但我添加的关键字不是我添加的关键字,它们显示为符号,但我必须关闭,因为正在输入某些内容,因为它向屏幕。看起来是这样的,\ " \ \ \ " \ \ \ \ \ \ \ " \ \ \ \ \ \ \ \ \
猜你喜欢
  • 2016-04-29
  • 2020-06-13
  • 2020-08-20
  • 2014-07-07
  • 1970-01-01
  • 2016-03-31
  • 2018-11-05
  • 1970-01-01
相关资源
最近更新 更多