Adding JSON to a Object Using JavaScript

I am creating a JSON object using JavaScript. How to insert the following data into the bottom of the stack:

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

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 this:

 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 does not work:

 var NewData = '"hello": { "label":"Hello", "url":"#hello" }'; ListData.push(NewData); 
+4
source share
5 answers

If you use jQuery, you can use. expand () jQuery API, for example:

 $.extend(ListData, {"hello": { "label":"Hello", "url":"#hello" }}); 
+4
source

You can insert it directly with the object literal:

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

I have another solution using the underscore.js module,

 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"}} 
+2
source

A JavaScript letter object is a list of name / value pairs separated by commas, wrapped in a pair of curly braces.

To add a property name for the encampment name with a Valley Forge value at the bottom of the stack, simply add the property name after the JSON object with a dot syntax. Then specify the value. (See "Adding Data" below)

You can also remove the added 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 
+1
source

Saving object statements with you literally adds another object to your ListData object.

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

push is for Javascript arrays only.

0
source

All Articles