这是我的密码.从几天开始,我一直在try 创建一个对象数组,然后将其存储在本地存储中.这就是问题所在,我需要首先从本地存储获取现有值.

I then need to add the new data object to the existing array. I then convert it into JSON so that I can store it back in the 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 the JSON to be an array of objects like this,

[{"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've been trying for several days and any help will be greatly appreciated.

推荐答案

该代码的问题是:

  1. 你将得到的结果包装在一个数组中,但理论上,你希望already有一个array.

  2. You're storing user, not get or abc. (You removed that with an edit.)

要存储数组,请执行以下操作:

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

To get the array:

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

请注意,如果getItem返回null,这将提供一个默认值(空数组),因为我们从未将用户存储在那里.

To add a user to the array:

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

示例(live on jsFiddle [Stack Snippets don't allow local storage]):

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

这会显示控制台中当前用户的列表,每次刷新页面时都会添加一个.

Json相关问答推荐

使用SQL查询从SON中查找第n个密钥对值

手动解开没有可编码的SON- Swift

使用JQ从jsonl文件中删除具有匹配键/值的行

当 JSON 字段名称有空格时,ABAP 中的 JSON 反序列化

使用 Redis 作为键值存储

将 json 转换为 jsonb 安全吗?

如何从 json 中获取单个元素?

ORA-01422: 精确提取返回的行数超过了与 json 对象组合的请求数

jq:来自嵌套 JSON 的映射

Kotlin Android Room 处理 Moshi TypeConverter 中的空对象列表

如何使用 CORS 实现 JavaScript Google Places API 请求

如何在 Eclipse 中安装和使用 JSON 编辑器?

Flask 请求和 application/json 内容类型

如何使用 Swift 从 NSURLSession 获取 cookie?

apple-app-site-association json 文件是否会在应用程序中更新?

waitUntilAllTask​​sAreFinished 错误 Swift

Sequelize - 如何仅返回数据库结果的 JSON 对象?

YAML 将 5e-6 加载为字符串而不是数字

如何使用 SwiftyJSON 遍历 JSON?

我如何反序列化以杰克逊为单位的时间戳?