[英]JSON: how do you add to a field that doesn't have a key?
I have a simple JSON object我有一个简单的 JSON 对象
simple_chart_config = {
chart: {
container: "#tree-simple"
},
nodeStructure: {
text: { name: "Question" },
children: [
{
text: { name: "Answer 1" }
}
]
}
};
And I'd like to add a new subfield within the first entry on the children
array so that the final output is我想在
children
数组的第一个条目中添加一个新的子字段,以便最终输出是
simple_chart_config = {
chart: {
container: "#tree-simple"
},
nodeStructure: {
text: { name: "Question" },
children: [
{
text: { name: "Answer 1" },
children: [
{
text: { name: "Question 2" }
}
]
}
]
}
};
I've tried several methods, such as我尝试了几种方法,例如
var questionTwoStr = '{"children": [{"text": { "name": "Question 2" }}]}'
var questionTwo = JSON.parse(questionTwoStr);
simple_chart_config.nodeStructure.children[0] = questionTwo;
but I'm having issues working out all of the nested indexes in my head.但是我在计算所有嵌套索引时遇到了问题。 This is for a tree in treant.js if that context is helpful at all.
如果上下文有帮助的话,这适用于 treant.js 中的树。
I think I'm mostly confused because the place I'm trying to add the new subfield doesn't have a key, which I thought was required for JSON.我想我很困惑,因为我试图添加新子字段的地方没有键,我认为这是 JSON 所必需的。
There's no need to use JSON here;这里不需要使用 JSON; you can add the object itself:
simple_chart_config.nodeStructure.children[0].children = [{text: { name:"Question 2" }}];
您可以添加对象本身:
simple_chart_config.nodeStructure.children[0].children = [{text: { name:"Question 2" }}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.