Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending to JSON object using JavaScript

I'm building the JSON object using JavaScript. How would I inset the following data to the bottom of the stack:

"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);
like image 458
Tony2K Avatar asked Oct 10 '10 01:10

Tony2K


5 Answers

You can insert it directly with an object literal:

ListData.hello = { label: "Hello", url: "#hello" }; 
like image 104
Clay Hinson Avatar answered Oct 31 '22 12:10

Clay Hinson


If you are using jQuery, you can use the .extend() jQuery API like:

$.extend(ListData, {"hello": { "label":"Hello", "url":"#hello" }});
like image 43
Alex Avatar answered Oct 31 '22 12:10

Alex


I have one more solution using 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"}}
like image 20
Sohan Avatar answered Oct 31 '22 13:10

Sohan


Keeping with you object literal statements just add another object to your ListData object.

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

push is only for Javascript Arrays.

like image 1
Bobby Borszich Avatar answered Oct 31 '22 11:10

Bobby Borszich


A JavaScript Object Literal is a comma-separated list of name/value pairs wrapped by a pair of curly braces.

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)

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
like image 1
Robert Bolton Avatar answered Oct 31 '22 11:10

Robert Bolton