简体   繁体   English

如何使用 localstorage 在数组中存储多个随机值

[英]How do i store multiple random values in an array with localstorage

I am trying to store multiple values, so I run a function that generates the values and then I store them with localStorage, however, I can't seem to get the values stored.我正在尝试存储多个值,因此我运行了一个 function 来生成这些值,然后将它们存储在 localStorage 中,但是,我似乎无法获取存储的值。 I added in 1, 2 and that seems to work but the randomly generated values don't.我在 1、2 中添加,这似乎有效,但随机生成的值无效。 From the image, you can see only 2 numbers have stored.从图像中,您可以看到仅存储了 2 个号码。

https://gyazo.com/a7180cac935809ca48c244794df3a6ca https://gyazo.com/a7180cac935809ca48c244794df3a6ca

$.getJSON("/products.json", function(product) {
  var value = [1, 2];
  console.log(value);

  localStorage["value"] = JSON.stringify(value);

  $.each(product.products, function() {
    var sMin = 5;
    var sMax = 20;

    var randomSelect = sMin + Math.floor(Math.random() * sMax);

    var t = randomSelect.toString();
    value.push(t);
  });
});

var test = JSON.parse(localStorage["value"]);
console.log(test);

I think you can use this method localStorage.getItem("key") .我认为您可以使用这种方法localStorage.getItem("key")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM