将数据附加到localStorage对象

时间:2016-03-12 21:15:26

标签: javascript jquery object local-storage

我尝试将新对象添加到当前的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);

4 个答案:

答案 0 :(得分:8)

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

d3.selectAll(".box")
    .data([800]) //I just used a hardcoded value for simplicity
        .append("circle")
        .attr("cx", chart.width()/2+margin.left)
        .attr("cy", function(d){return chart.x1(d);}) // using same scale that was used to draw the box plot.
        .attr("r", 5)

答案 1 :(得分:4)

当你从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))

答案 2 :(得分:2)

您使用Object.assign()错误。有关它的信息,请参阅here

您真的需要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);

答案 3 :(得分:1)

根据MDN The Object.assign() method only copies enumerable and own properties from a source object to a target object

在你的情况下: var stored = JSON.parse(retrievedObject);返回数组,您只需push新对象到数组:stored.push(newStudent2);,并将stored设置为localStorage