wongadob Posted March 11, 2012 Share Posted March 11, 2012 I am trying to add another element to an associative array, but cannot figure out the correct syntax. var sendData = new Array();sendData= {body: message, replied_to_id: id};if (group != null){sendData+={group_id: group};} I am pretty sure that syntax is wrong, but I am also sure I have tried every combination I can think of. Sorry its a newbie question but That is me! Link to comment Share on other sites More sharing options...
Ingolme Posted March 11, 2012 Share Posted March 11, 2012 sendData.group_id = group By the way, that's not an array, it's an object. Your second line of code is making the new Array() line useless. There are no associative arrays in Javascript. Link to comment Share on other sites More sharing options...
wongadob Posted March 11, 2012 Author Share Posted March 11, 2012 I tried changing that line to sendData = new Object as I had read that but it just stopped working adding the initial values. So should the initial definition be new Object ? Link to comment Share on other sites More sharing options...
wongadob Posted March 11, 2012 Author Share Posted March 11, 2012 S'ok all working now. Thanks! I dont know why last time I changed it to new object it didn't work as it did this time! Link to comment Share on other sites More sharing options...
ShadowMage Posted March 12, 2012 Share Posted March 12, 2012 You actually don't need the first line at all. The second line is just overwriting the new Object you created with another new Object, not adding properties to an existing Object. If you just want to add properties you use the syntax Ingolme showed. Link to comment Share on other sites More sharing options...
thescientist Posted March 12, 2012 Share Posted March 12, 2012 Also, there's really no reason to use the new keyword when literal syntax will work just as easy, and without the overhead of calling the constructor myArray = []; //new Array()myObject = {}; //new Object()myString = ""; //new String("");myNum = 3; //new Number(3);myBool = true; //new Boolean; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.