gpt4 book ai didi

javascript - 使用 JavaScript 附加到 JSON 对象

转载 作者:数据小太阳 更新时间:2023-10-29 05:40:51 26 4
gpt4 key购买 nike

我正在使用 JavaScript 构建 JSON 对象。我如何将以下数据插入堆栈底部:

"hello": { "label":"Hello", "url":"#hello" }

进入以下变量:

var ListData = {
"main": {
"label":"Main",
"url":"#main"
},
"project": {
"label":"Project",
"url":"#project"
},
"settings": {
"label":"Settings",
"url":"#settings",
"subnav":[
{
"label":"Privacy",
"url":"#privacy"
},
{
"label":"Security",
"url":"#security"
},
{
"label":"Advanced",
"url":"#advanced"
}
]
}
};

所以变量看起来像:

var ListData = {
"main": {
"label":"Main",
"url":"#main"
},
"project": {
"label":"Project",
"url":"#project"
},
"settings": {
"label":"Settings",
"url":"#settings",
"subnav":[
{
"label":"Privacy",
"url":"#privacy"
},
{
"label":"Security",
"url":"#security"
},
{
"label":"Advanced",
"url":"#advanced"
}
]
},
"hello": {
"label":"Hello",
"url":"#hello"
}
};

我使用了以下代码,但它似乎不起作用:

var NewData = '"hello": { "label":"Hello", "url":"#hello" }';
ListData.push(NewData);

最佳答案

您可以直接使用对象字面量插入它:

ListData.hello = { label: "Hello", url: "#hello" }; 

关于javascript - 使用 JavaScript 附加到 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3898563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com