I'm building the JSON object using JavaScript. How would I inset the following data to the bottom of the stack:
我正在使用JavaScript构建JSON对象。我如何将以下数据插入堆栈的底部:
"hello": { "label":"Hello", "url":"#hello" }
in to the following variable:
在以下变量中:
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"
}
]
}
};
So the variable looks like:
所以变量看起来像:
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"
}
};
I used the following code but it doesn't seem to work:
我使用了以下代码,但它似乎不起作用:
var NewData = '"hello": { "label":"Hello", "url":"#hello" }';
ListData.push(NewData);
5 个解决方案
#1
5
If you are using jQuery, you can use the .extend() jQuery API like:
如果您使用的是jQuery,则可以使用.extend()jQuery API,如:
$.extend(ListData, {"hello": { "label":"Hello", "url":"#hello" }});
#2
14
You can insert it directly with an object literal:
您可以直接使用对象文字插入它:
ListData.hello = { label: "Hello", url: "#hello" };
#3
2
I have one more solution using underscore.js module,
我还有一个使用underscore.js模块的解决方案,
var _ = require("underscore");
var src = {
"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 dest = {"hello": { "label":"Hello", "url":"#hello" }};
var data = _.extend(src, dest);
console.log(JSON.stringify(data));
Required op :
要求的操作:
{"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"}}
#4
1
A JavaScript Object Literal is a comma-separated list of name/value pairs wrapped by a pair of curly braces.
JavaScript Object Literal是由一对花括号包装的以逗号分隔的名称/值对列表。
To append the property name of encampment name with a value of Valley Forge to the bottom of the stack, simply add the property name after the JSON object with a dot syntax. Then specify the value. (See 'Append data' below)
要将具有Valley Forge值的encampment name的属性名称附加到堆栈的底部,只需使用点语法在JSON对象后添加属性名称。然后指定值。 (参见下面的“附加数据”)
You can also delete the appended name/value pair from the object literal. (See 'Delete data below')
您还可以从对象文字中删除附加的名称/值对。 (参见'删除下面的数据')
// Start with some JSON
var myJson = { "name":"George Washington", "rank":"General", "serial":"102" };
// Append data
myJson.encampment = "Valley Forge";
// Delete data
delete myJson.encampment
#5
0
Keeping with you object literal statements just add another object to your ListData object.
保持对象文字语句只需向ListData对象添加另一个对象。
ListData.hello = { "label":"Hello", "url":"#hello" };
push is only for Javascript Arrays.
push仅适用于Javascript Arrays。
#1
5
If you are using jQuery, you can use the .extend() jQuery API like:
如果您使用的是jQuery,则可以使用.extend()jQuery API,如:
$.extend(ListData, {"hello": { "label":"Hello", "url":"#hello" }});
#2
14
You can insert it directly with an object literal:
您可以直接使用对象文字插入它:
ListData.hello = { label: "Hello", url: "#hello" };
#3
2
I have one more solution using underscore.js module,
我还有一个使用underscore.js模块的解决方案,
var _ = require("underscore");
var src = {
"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 dest = {"hello": { "label":"Hello", "url":"#hello" }};
var data = _.extend(src, dest);
console.log(JSON.stringify(data));
Required op :
要求的操作:
{"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"}}
#4
1
A JavaScript Object Literal is a comma-separated list of name/value pairs wrapped by a pair of curly braces.
JavaScript Object Literal是由一对花括号包装的以逗号分隔的名称/值对列表。
To append the property name of encampment name with a value of Valley Forge to the bottom of the stack, simply add the property name after the JSON object with a dot syntax. Then specify the value. (See 'Append data' below)
要将具有Valley Forge值的encampment name的属性名称附加到堆栈的底部,只需使用点语法在JSON对象后添加属性名称。然后指定值。 (参见下面的“附加数据”)
You can also delete the appended name/value pair from the object literal. (See 'Delete data below')
您还可以从对象文字中删除附加的名称/值对。 (参见'删除下面的数据')
// Start with some JSON
var myJson = { "name":"George Washington", "rank":"General", "serial":"102" };
// Append data
myJson.encampment = "Valley Forge";
// Delete data
delete myJson.encampment
#5
0
Keeping with you object literal statements just add another object to your ListData object.
保持对象文字语句只需向ListData对象添加另一个对象。
ListData.hello = { "label":"Hello", "url":"#hello" };
push is only for Javascript Arrays.
push仅适用于Javascript Arrays。