【问题标题】:Insert new group into JSON using Javascript使用 Javascript 将新组插入 JSON
【发布时间】:2017-12-31 16:07:36
【问题描述】:

var module = {
  "code": "6COM9051",
  "name": "More Web Scripting",
  "cohort": "IT/JH/ITMB",
  "students": {
    "97xx08xx01": {
      "firstName": "Jo 1",
      "lastName": "Bloggs",
      "allocatedGroup": "690511"
    },
    "97xx08xx03": {
      "firstName": "Jo 2",
      "lastName": "Bloggs",
      "allocatedGroup": "690511"
    },
    "97xx08xx02": {
      "firstName": "Jo 3",
      "lastName": "Bloggs",
      "allocatedGroup": "690513"
    },
    "97xx08xx06": {
      "firstName": "Joe 1",
      "lastName": "Bliggs",
      "allocatedGroup": "690513"
    },
    "97xx08xx05": {
      "firstName": "Joe 2",
      "lastName": "Bliggs",
      "allocatedGroup": "690511"
    },
    "97xx08xx04": {
      "firstName": "Joe 3",
      "lastName": "Bliggs",
      "allocatedGroup": "690513"
    },
  },
  "groups": {
    "690511": {
      "name": "Tutorial 1",
      "members": ["97xx08xx01", "97xx08xx05", "97xx08xx03"]
    },
    "690513": {
      "name": "Tutorial 2",
      "members": ["97xx08xx04", "97xx08xx02", "97xx08xx06"]
    }
  }
}

【问题讨论】:

标签: javascript json


【解决方案1】:

你可以只赋值,

modules.groups["<group key>"] = {
    "name": "Tutorial 123",
    "members": ["97xx08xx01", "97xx08xx05", "97xx08xx03"]
}

PS:我认为您是 stackoverflow 的新手。虽然我回答了这个问题,但这个社区不鼓励这种类型的问题。 如何向 JSON 添加属性 是一个非常基本的事情,在数百万个教程中都有解释,并且可能在几年前的 stackoverflow 上得到了回答。在寻求帮助之前做好功课。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2021-09-29
    • 2018-03-07
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    相关资源
    最近更新 更多