开发者问题收集

将数据附加到 localStorage 对象

2016-03-12
35537

我尝试向当前 localStorage 对象添加新对象,但没有成功。最后,localStorage 中没有两组数据,而是只得到了最后一组。有人知道我做错了什么吗?谢谢

以下是我想做的事情:

// add the first student
var newStudent = [{
     "name":        "John",
     "age":         21,
     "nationality": "Spanish"
 }];

localStorage.setItem("students", JSON.stringify(newStudent));


// Retrieve the object from storage to add a new student
var retrievedObject = localStorage.getItem("students");
var stored          = JSON.parse(retrievedObject);

// add a new student
var newStudent2 = [{
    "name":        "Mary",
    "age":         20,
    "nationality": "German"
}];
var stored = Object.assign(stored, newStudent2);

// Update the storage
localStorage.setItem("students", JSON.stringify(stored));
var result = localStorage.getItem("students");

console.log(result);
3个回答

您应该存储数组,而不是对象;

var students = [];

var student1 = { s: 1 };

students.push(student1);

localStorage.setItem("students", JSON.stringify(students));

var stored = JSON.parse(localStorage.getItem("students"));

var student2 = { s: 2 };

stored.push(student2);

localStorage.setItem("students", JSON.stringify(stored));

var result = JSON.parse(localStorage.getItem("students"));

console.log(result);
Sergey Yarotskiy
2016-03-12

从 localStorage 中取回存储的对象时,您正在用 newStudent2 替换它:

var newStudent = [{
     "name":        "John",
     "age":         21,
     "nationality": "Spanish"
 }];

localStorage.setItem("students", JSON.stringify(newStudent));

var retrievedObject = localStorage.getItem("students");
var stored          = JSON.parse(retrievedObject); <----newStudent1

var newStudent2 = [{
    "name":        "Mary",
    "age":         20,
    "nationality": "German"
}];


var stored = Object.assign(stored, newStudent2); <----Here newStudent1 is replaced by newStudent2


localStorage.setItem("students", JSON.stringify(stored)); // Here newStudent2 is replacing old object on localStorage
var result = localStorage.getItem("students");

console.log(result);

您可以尝试创建一个对象数组,并在每次创建新对象时附加它们。

var objects = []
objects.push(stored) 

localStorage.setItem('students', JSON.stringify(objects))
Diego Gallegos
2016-03-12

您错误地使用了 Object.assign() 。有关详细信息,请参阅 此处

您真的需要 newStudent2 成为单个对象的数组吗?如果不是,您可以简单地执行 stored.push(newStudent2) ,其中 newStudent2 是一个对象,而不是一个包含单个对象的数组。

因此,类似于:

var students = [];

// add the first student
// Notice how the student is now an object and not an array containing an object.
var newStudent = {
     "name":        "John",
     "age":         21,
     "nationality": "Spanish"
 };

students.push(newStudent);

localStorage.setItem("students", JSON.stringify(students));


// Retrieve the object from storage to add a new student
var retrievedObject = localStorage.getItem("students");
var stored          = JSON.parse(retrievedObject);

// add a new student
// Notice how the student is now an object and not an array containing an object.
var newStudent2 = {
    "name":        "Mary",
    "age":         20,
    "nationality": "German"
};

stored.push(newStudent2);

// Update the storage
localStorage.setItem("students", JSON.stringify(stored));
var result = localStorage.getItem("students");

console.log(result);
Dimitris Karagiannis
2016-03-12