SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    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
    Last edited by dele454; Mar 4, 2009 at 05:28.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    event.prop = 'foo';
    // or
    event['prop'] = 'foo';

  3. #3
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks mate

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

    So this works too thanks


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

  4. #4
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
     [Object, "141", undefined, "123"]
    if i change it to

    Code:
     events = checks[i].value;
    I only see the last item which makes sense

    Then i tried
    Code:
     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

    Code:
    ...
    
    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

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    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:

    Code javascript:
    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);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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()?

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    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:

    Code javascript:
    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
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Working perfectly now. The array seems to be converted to a json object on declaring ;handleAS: json'

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

  9. #9
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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


    Thanks a mil!!!

  10. #10
    Strokin' Morango dele454's Avatar
    Join Date
    Oct 2005
    Location
    Cape town, South Africa
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •