Append data to localStorage object

后端 未结 4 644
夕颜
夕颜 2021-02-10 11:08

I\'m trying, unsuccessfully, to add a new object to a current localStorage object. Instead of, at the end, having two sets of data in localStorage, I get the last one. Any insig

相关标签:
4条回答
  • 2021-02-10 11:32

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

    in your case: var stored = JSON.parse(retrievedObject); return array, you can just push new object to array: stored.push(newStudent2); and when set stored to localStorage.

    0 讨论(0)
  • 2021-02-10 11:33

    You should store array, not an object;

    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);
    
    0 讨论(0)
  • 2021-02-10 11:42

    You are using Object.assign() wrong. See here for info about it.

    Do you really need newStudent2 to be an array of a single object? If not you can simply do stored.push(newStudent2), where newStudent2 is an object and not an array with a single object.

    So, something like:

    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);
    
    0 讨论(0)
  • 2021-02-10 11:47

    You are replacing the stored object by newStudent2 when fetching it back from localStorage:

    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);
    

    You can instead try creating an array of objects and appending them whenever creating a new one.

    var objects = []
    objects.push(stored) 
    
    localStorage.setItem('students', JSON.stringify(objects))
    
    0 讨论(0)
提交回复
热议问题