简体   繁体   English

将更多数据保存到本地存储

[英]Saving more data to local storage

I know it is best way to use JSON but it is too complex for me to understand. 我知道这是使用JSON的最佳方法,但是对我来说太复杂了。 Thus, may i ask is there any other ways to make sure that my data does not overwrite in local storage. 因此,请问还有其他方法可以确保我的数据不会在本地存储中被覆盖。 Example using for loop or using another key name. 使用for循环或使用另一个键名的示例。 Please help me by giving my examples as i am very new to HTML/JavaScript. 请举我的示例来帮助我,因为我是HTML / JavaScript的新手。

function saveToLS(){

    var Name = document.getElementById("rName");
    var namesaved = Name.value;
    localStorage.setItem("Name",namesaved);


    var Comment = document.getElementById("rComment");
    var commentsaved = Comment.value;
    localStorage.setItem("Comment",commentsaved);   


}

Are you going to store multiples of these? 您要存储这些的倍数吗? You should store them in an array or object (for example adding a timestamp id field) and store that array in LS. 您应该将它们存储在数组或对象中(例如,添加时间戳记ID字段),然后将该数组存储在LS中。

The best option is using JSON. 最好的选择是使用JSON。 JSON is not very complex. JSON不是很复杂。 It's just a string which is a result of stringification of a language construct like an array or object. 它只是一个字符串,是字符串(如数组或对象)的语言结构字符串化的结果。

What you need is an array: [] , stringified version of it: "[]" . 您需要一个数组: [] ,它的字符串化版本: "[]" So you need to push an element into the array. 因此,您需要将元素推入数组。 How? 怎么样? Convert the stored JSON string into an array by parsing it, and then push an element and stringify the new array. 通过解析存储的JSON字符串将其转换为数组,然后推送一个元素并对新数组进行字符串化。 Here is an example: 这是一个例子:

var names = localStorage.getItem("Name");
names = names ? JSON.parse(names) : [];
names.push('newName');

localStorage.setItem("Name", JSON.stringify(names));

Also don't use 2 keys for storing 2 datum that belongs to one item. 也不要使用2个键来存储属于一项的2个基准。 It will be hard to maintain. 这将很难维护。 Use an object, an array of objects: 使用一个对象,一个对象数组:

comments.push({
  author: 'name of the author',
  comment: 'here goes the comment body...'
})

 Yeah..Me Also new In this Part... but I hope to you help full this code please try It.. var key = 0; for(key;key<10;key++){ localStorage.setItem(key, localStorage.getItem(key) + "Datas"); } 

function saveToLS(){

var Name = document.getElementById("rName");
var namesaved = Name.value;
if(localStorage.setItem("Name").length){   //the data already exists
     // Do whatever if the data already Exists......
}else{
    localStorage.setItem("Name",namesaved);
}


var Comment = document.getElementById("rComment");
var commentsaved = Comment.value;


if(localStorage.setItem("Comment").length){ //the data already exists
     // Do whatever if the data already Exists......
}else{
    localStorage.setItem("Comment",commentsaved);
}   

} }

check using localStorage.getItem(), if the data already exists and if its true don't write the data again or for that matter do whatever you want like save the data with another key 使用localStorage.getItem()检查数据是否已经存在,如果数据为true,则不要再次写入数据,或者执行任何操作,如使用另一个键保存数据

if(localStorage.setItem("Name").length){   //the data already exists
 localStorage.setItem("SomethingElse...",namesaved); //something else
}else{
   localStorage.setItem("Name",namesaved); 
}

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

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