Adding Items To A JSON Object - JavaScript - SitePoint Forums Home » Add Element Json Jquery » Adding Items To A JSON Object - JavaScript - SitePoint Forums Maybe your like A L'intérieur Film Horreur Add Element Json List Alin Vl Youtube Add Element Json Php Ali ömür Qm Formasyonu Adding items to a JSON object JavaScript dele454 March 3, 2009, 8:31pm 1 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 crmalibu March 3, 2009, 8:38pm 2 event.prop = 'foo'; // or event['prop'] = 'foo'; dele454 March 3, 2009, 8:50pm 3 Thanks mate I needed to add items to JSON object in a for loop. So this works too thanks 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 dele454 March 4, 2009, 11:53am 4 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 Paul_Wilkins March 4, 2009, 12:53pm 5 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); dele454 March 4, 2009, 4:17pm 6 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()? Paul_Wilkins March 4, 2009, 4:21pm 7 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 dele454 March 4, 2009, 4:24pm 8 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 dele454 March 4, 2009, 4:27pm 9 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!!! dele454 March 4, 2009, 4:29pm 10 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!! Related topics Topic Replies Views Activity Adding item to array in JSON JavaScript 6 99432 October 21, 2011 Array +JSON +javascript JavaScript 7 1215 October 19, 2011 JavaScript array to JSON JavaScript 10 4671 January 14, 2018 Newbie JSON question JavaScript 2 675 March 15, 2016 JSON -- this one has me stumped JavaScript 10 1344 October 8, 2014 Tag » Add Element Json Jquery Adding/removing Items From A JavaScript Object With JQuery Jquery Add New Node To Json Object Code Example Jquery Add New Property To Json Object Code Example tJSON() | JQuery API Documentation rseJSON() | JQuery API Documentation .data() | JQuery API Documentation How To Add Data In JSON File Using Node.js ? - GeeksforGeeks JQuery Add Elements - W3Schools How To Add A New Array Element To A JSON Object With JavaScript? How Can I Append Json Object To JQuery And JSON - Add Element How To Convert HTML Form Field Values To A JSON Object FormData Append Javascript Array (ringify), How To Cast As ... Access And Print A Specific JSON Value | Documenting APIs Select Values From A JSON Object Using JQuery - W3resource I Want To Add A New JSON Object To The Already Existing JSON Array ... Manipuler Des Données JSON - Apprendre Le Développement Web Add To Array Javascript Working With JSON Object With Examples - Dot Net Tutorials