8

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);

5 Answers 5

21

You can insert it directly with an object literal:

ListData.hello = { label: "Hello", url: "#hello" }; 
Sign up to request clarification or add additional context in comments.

Comments

9

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

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

1 Comment

After nine and a half years, this solution helped me a lot. Thank you.
2

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"}}

Comments

2

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.