Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested JSON: How to add (push) new items to an object?

I'm just starting with Arrays, Objects, and JSON - so hopefully there's just something simple I'm overlooking here. I'm encountering an error when attempting to add (push) a new item into my json object.

I'm encountering the following error: Result of expression 'library.push' [undefined] is not a function (towards the bottom of my code snippet).

// This is my JSON object generated from a database var library = {     "Gold Rush" : {         "foregrounds" : ["Slide 1","Slide 2","Slide 3"],         "backgrounds" : ["1.jpg","","2.jpg"]     },     "California" : {         "foregrounds" : ["Slide 1","Slide 2","Slide 3"],         "backgrounds" : ["3.jpg","4.jpg","5.jpg"]     } }  // These will be dynamically generated vars from editor var title = "Gold Rush"; var foregrounds = ["Howdy","Slide 2"]; var backgrounds = ["1.jpg",""];  function save () {      // If title already exists, modify item     if (library[title]) {         // Replace values with new         library[title].foregrounds = foregrounds;         library[title].backgrounds = backgrounds;          // Save to Database. Then on callback...         document.write('Changes Saved to <b>'+title+'</b>');      // If title does not exist, add new item     else {         // Format it for the JSON object         var item = ('"'+title+'" : {"foregrounds" : '+foregrounds+',"backgrounds" : '+backgrounds+'}');           // THE PROBLEM SEEMS TO BE HERE??         // Error: "Result of expression 'library.push' [undefined] is not a function"         library.push(item);           // Save to Database. Then on callback...         document.write('Added: <b>'+title+'</b>');     } }  save(); 
like image 880
Josiah Avatar asked Jun 28 '11 07:06

Josiah


People also ask

How do I add data to an existing JSON object?

Use push() method to add JSON object to existing JSON array in JavaScript. Just do it with proper array of objects .

Can we use push method on object?

push can work on an object just fine, as this example shows. Note that we don't create an array to store a collection of objects. Instead, we store the collection on the object itself and use call on Array. prototype.

Can JSON objects include nested objects?

Objects can be nested inside other objects. Each nested object must have a unique access path. The same field name can occur in nested objects in the same document.

How do you push an object in JavaScript?

JavaScript Array push()The push() method adds new items to the end of an array. The push() method changes the length of the array. The push() method returns the new length.


2 Answers

library is an object, not an array. You push things onto arrays. Unlike PHP, Javascript makes a distinction.

Your code tries to make a string that looks like the source code for a key-value pair, and then "push" it onto the object. That's not even close to how it works.

What you want to do is add a new key-value pair to the object, where the key is the title and the value is another object. That looks like this:

library[title] = {"foregrounds" : foregrounds, "backgrounds" : backgrounds}; 

"JSON object" is a vague term. You must be careful to distinguish between an actual object in memory in your program, and a fragment of text that is in JSON format.

like image 102
Karl Knechtel Avatar answered Oct 04 '22 11:10

Karl Knechtel


If your JSON is without key you can do it like this:

library[library.length] = {"foregrounds" : foregrounds,"backgrounds" : backgrounds}; 

So, try this:

var library = {[{     "title"       : "Gold Rush",         "foregrounds" : ["Slide 1","Slide 2","Slide 3"],         "backgrounds" : ["1.jpg","","2.jpg"]     }, {     "title"       : California",         "foregrounds" : ["Slide 1","Slide 2","Slide 3"],         "backgrounds" : ["3.jpg","4.jpg","5.jpg"]     }] } 

Then:

library[library.length] = {"title" : "Gold Rush", "foregrounds" : ["Howdy","Slide 2"], "backgrounds" : ["1.jpg",""]}; 
like image 30
Rodrigo Durán Roubillard Avatar answered Oct 04 '22 11:10

Rodrigo Durán Roubillard