javascript 将对象添加到 localStorage 中的数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19635077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adding objects to array in localStorage
提问by Joh Smith
Ok, I've tried to follow examples here, I know there might be a few different ways of adding objects to an array in localstorage and not overwriting it but I'm failing to find at least one of them.
好的,我已经尝试在这里遵循示例,我知道可能有几种不同的方法可以将对象添加到 localstorage 中的数组中而不是覆盖它,但我至少找不到其中一种。
Got this code to store objects in array but it's overwriting itself. May anyone show me what am I missing? (And I'm afraid I could be missing a lot).
得到了这段代码来将对象存储在数组中,但它正在覆盖自己。谁能告诉我我错过了什么?(而且我担心我可能会错过很多)。
function addEntry() {
var entryTitle = document.getElementById("entryTitle").value;
var entryText = document.getElementById("entryText").value;
var entry = {
"title": entryTitle,
"text": entryText
};
localStorage.setItem("entry", JSON.stringify(entry));
var allEntries = [];
allEntries.push(entry);
localStorage.setItem("allEntries", JSON.stringify(allEntries));
};
回答by CodingIntrigue
When you use setItem
it overwrites the item which was there before it. You need to use getItem
to retrieve the old list, append to it, then save it back to localStorage:
当您使用setItem
它时,它会覆盖它之前的项目。您需要使用getItem
来检索旧列表,附加到它,然后将其保存回 localStorage:
function addEntry() {
// Parse any JSON previously stored in allEntries
var existingEntries = JSON.parse(localStorage.getItem("allEntries"));
if(existingEntries == null) existingEntries = [];
var entryTitle = document.getElementById("entryTitle").value;
var entryText = document.getElementById("entryText").value;
var entry = {
"title": entryTitle,
"text": entryText
};
localStorage.setItem("entry", JSON.stringify(entry));
// Save allEntries back to local storage
existingEntries.push(entry);
localStorage.setItem("allEntries", JSON.stringify(existingEntries));
};
Here is a fiddlewhich demonstrates the above.
这是一个演示上述内容的小提琴。
回答by David Hellsing
Maybe you just need to fetch the entries before pushing the new one:
也许您只需要在推送新条目之前获取条目:
var allEntries = JSON.parse(localStorage.getItem("allEntries")) || [];
allEntries.push(entry);
//etc...
回答by arslan
const object = {
name: 'ayyan',
age: 29,
number: 03070084689,
};
const arr = JSON.parse(localstorage.getItem('key_name')) || [];
arr.push(object);
localstorage.setitem(json.strigify('key_name', arr);
回答by Azeem Aslam
HTML5 localStorage lets you store key/value pairs of data. Both key and value needs to be a string. To store arrays as a key or value you need to encode the array into a JSON string. And while retrieving you need to decode it back to an array.
HTML5 localStorage 允许您存储数据的键/值对。key 和 value 都需要是 string。要将数组存储为键或值,您需要将数组编码为 JSON 字符串。在检索时,您需要将其解码回数组。
const object = {
name: 'ayyan',
age: 29,
number: 03070084689,
};
const arr = JSON.parse(localstorage.getItem('key_name')) || [];
const data = [arr, ...[object]];
localstorage.setitem(json.strigify('key', data);
回答by mahendren
adding objects to array in localStorage (Ionic):
将对象添加到 localStorage (Ionic) 中的数组:
var existingEntries = JSON.parse(localStorage.getItem("allEntries"));
if(existingEntries == null) existingEntries = [];
var testObject ={username:this.username,
mobile:this.mobile,
email:this.email,
type:this.type,
password:this.password};
localStorage.setItem('testObject', JSON.stringify(testObject));
existingEntries.push(testObject);
localStorage.setItem("allEntries", JSON.stringify(existingEntries));