How to store an array of objects in local storage?

This is my code. I have been trying to create an array of objects from several days, which I will store in local storage. Here is the problem: I need to get the existing value first from local storage.

Then I need to add a new data object to an existing array. Then I convert it to JSON to save it back to local storage.

onRegisterSubmit(){
    const user = {
        a: this.a,
        b: this.b,
        c: this.c,
      id: Date.now()
    }    

   var abc = [];
   var get =  JSON.parse(localStorage.getItem('user'));
   abc = [get];
   abc.push(user);

   localStorage.setItem('user', JSON.stringify(abc));

   console.log(JSON.stringify(abc));
   console.log(get);
  }

I want JSON to be an array of such objects,

[{"hour":1,"minute":21,"ampm":"PM","repeatDays":[],"message":"","todayOrTomorrow":"Tomorrow","isRepeatMode":false,"isEnabled":false,"id":"1493797882440"},{"hour":1,"minute":24,"ampm":"PM","repeatDays":[],"message":"","todayOrTomorrow":"Tomorrow","isRepeatMode":false,"isEnabled":false,"id":"1493797896257"},{"hour":6,"minute":14,"ampm":"PM","repeatDays":[],"message":"","todayOrTomorrow":"Tomorrow","isRepeatMode":false,"isEnabled":false,"id":"1493815470408"}]

This is my JSON.

[[[[[[[{"id":1493820594019},{"id":1493820606448}],{"id":1493820609111}],{"id":1493820610150}],{"id":1493820610553}],{"id":1493820610827}],{"id":1493820611015}],{"id":1493820612018}]

I tried a few days and any help would be greatly appreciated.

+6
source share
3 answers

Problems with this code:

  • You complete the result obtained in the array, but theoretically you want to have an array.

  • user, get abc. ( .)

, , :

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

:

users = JSON.parse(localStorage.getItem("users") || "[]");

, ( ), getItem null, .

:

users.push({id: 1, foo: "bar"});

(live on jsFiddle [ ]):

(function() { // Scoping function to avoid creating globals
    // Loading
    var users = JSON.parse(localStorage.getItem("users") || "[]");
    console.log("# of users: " + users.length);
    users.forEach(function(user, index) {
        console.log("[" + index + "]: " + user.id);
    });

    // Modifying
    var user = {
        id: Math.floor(Math.random() * 1000000)
    };
    users.push(user);
    console.log("Added user #" + user.id);

    // Saving
    localStorage.setItem("users", JSON.stringify(users));
})();

, , .

+5

- : link https://jsfiddle.net/sureshraina/nLexkyfw/1/

var mydatas = new Array();
        mydatas[0] = "data";
        mydatas[1] = "data1";
        mydatas[2] = "data2";

        localStorage["mydatas"] = JSON.stringify(mydatas);

        var datas = JSON.parse(localStorage["mydatas"]); 
+1

See this post.

You cannot store objects, you need to save the string. Thus, a workaround is to compose your object before saving it (for example, you could use it to modify the JSON object, save it and, if necessary, read it again).

0
source

Source: https://habr.com/ru/post/1016959/


All Articles