Adding items to a JSON object

hi,

Does anyone know the function for adding more items to an already declared JSON object?

Assuming you have something like:


var event= [{}]; // this is the correct empty JSON declaration


how can i add an itemt to the object?

I used push but not the right function.

Thanks


event.prop = 'foo';
// or
event['prop'] = 'foo';

Thanks mate :slight_smile:

I needed to add items to JSON object in a for loop.

So this works too thanks :slight_smile:


 events[i] = checks[i].value; // assuming [i] is the iterator
console.log(events.json); // prints something like  
Object 0=value 1=value


Thanks a mil :slight_smile:

Not correct!! The line event[i] is wrong. When i send ‘events’ to console i get something this - iterating over a numer of variables and adding their values to the JSON object:

 [Object, "141", undefined, "123"]

if i change it to

 events = checks[i].value;

I only see the last item which makes sense

Then i tried

 events += checks[i].value;

And i get

[object Object]141167. // 141 being the first item and 167 second item. But will like to have the object as [141,167] - how can i do that when adding to the object???

Here is some of my code to clear this up a bit


...

var events = [{}]; // empty JSON object
			
		var checks = dojo.query("input[name=item]");
		var length = checks.length;
		
		
				for(var i=0; i < length; i++){		
					
					if(checks[i].checked == true){
						
					
					   events += checks[i].value;
					 }
					
					 				
				  }
				
			
			  console.log(events);


I reckon the object Object item that is part of the JSON’s values comes from the fact that i declared and empty JSON object from the start.

How can i recitify this- just the right values showing on events?

Thanks

The events variable starts out as an array, and item 0 of that array is an object that is empty.

As you are wanting the checked values to be stored in an array, as [141, 167], here’s how you would do it:


var events = [], // empty array
    checks = dojo.query("input[name=item]"),
    checksLen = checks.length,
    i;
for (i = 0; i < checksLen; i += 1) {
    if (checks[i].checked === true) {
        events.push(checks[i].value);
    }
}
console.log(events);

Thanks for this. But i want to use a json object because i need to pass it to my PHP script.

Is there a way of converting a javascript array to a JSON Object. toJSONString()?

That’s where you stringify the object.

If your library doesn’t handle it, the code is linked at the bottom of this page can do it for you:
http://www.json.org/js.html

Still, if you want them as properties of the object in an array, here’s how the above code changes to achieve that:


var events = [{}], // empty array
    checks = dojo.query("input[name=item]"),
    checksLen = checks.length,
    i;
for (i = 0; i < checksLen; i += 1) {
    if (checks[i].checked === true) {
        events[0][i] = checks[i].value;
    }
}
console.log(events);

That will result in something like the following structure
[{
0: ‘141’,
1: ‘167’
}]

Edit:

Correction: resulting values were shown as numbers, and are now shown as strings

Working perfectly now. The array seems to be converted to a json object on declaring ;handleAS: json’


dojo.xhrPost({
				 
				 url: "/member/delete-member-events",
				 handleAs: "json",
				 postData: "&events=" + events,
				 load: function(response){
					 
					 dojo.byId("response"). innerHTML = response;	 
					 
					 return response;
				 }
				
				 });

I just thought i needed to create an empty json object, insert the values the call the xhrPost request.But now i know it can be an array the dojo.parser will always convert it for me.

Thanks for all your help :slight_smile:

That will result in something like the following structure
[{
0: 141,
1: 167
}]

That is more like what i was looking for - perfect!! but i’ll note the stringify method for future use. I’ll just stick to the simple array for now :slight_smile:

Thanks a mil!!! :slight_smile:

for (i = 0; i < checksLen; i += 1) {
if (checks[i].checked === true) {
events[0][i] = checks[i].value;
}
}

i see where i missed it now ‘events[0][i]’ - crafty!! :slight_smile: