View Full Version : How do I push new values to the following javascript array?

07-16-2012, 09:57 PM
How do I push new values to the following javascript array?

json = {"cool":"34.33","alsocool":"45454"}

I tried json.push("coolness":"34.33"); but it didn't work.

07-16-2012, 10:19 PM
It's a generic object, not an array. You can add a property to an object using the following statement:


07-16-2012, 11:39 PM
I'd advisde against callingyour object json as it would be too easy to get it confused with the JSON object built into JavaScript.

07-18-2012, 09:13 PM
Sorry I should have been more clear you see this array below, notice it has two separate user values in there. If i wanted to add a third user value into this array with those same fields how would i do that? Thanks you guys/girls btw!

var contacts = [{"LNAME":"Saula.","FNAME":"Sir","MIDDLE":"","TITLE":"Ph.D.","JOB":"Academic Dean of Engineering","SECTION":"Dean's Office","EMAIL":"sirsaulajr@tasdsau.edu","IMG":"/coe/dean/},{"LNAME":"sdsada","FNAME":"John","MIDDLE":"bab","TITLE":"Ph.D.","JOB":"Senior Associate Dean","SECTION":"Dean's Office","EMAIL":"basdas@masma.com","IMG":"/coe/dean/images/ "}];

07-18-2012, 09:33 PM
Nevermind thanks guys figured it out!

And for anyone in the future googling, yahooing, or binging this..

It is done this way

contacts.push({"LNAME":"sdsada","FNAME":"John","MIDDLE":"bab","TITLE":"Ph.D.","JOB":"Senior Associate Dean","SECTION":"Dean's Office","EMAIL":"basdas@masma.com","IMG":"/coe/dean/images/"});

07-18-2012, 10:03 PM
hmm last question; I am trying to push to the javascript array after looping 200 times through the member array to push every member that matches the department selected description into the javascript array., However it fails

<? $i = 0 ;
while( $i < 200)

if ($members[$i]['department'] == $depttts){
echo "<script type='text/javascript'>contacts.push({"NAME":"SirSaula","TITLE":"John","CONTACT":"bab","RESEARCH":"Ph.D."});</script>";

else {



Old Pedant
07-18-2012, 10:35 PM
And this has what to do with JavaScript???

That's pure PHP code.

But it certainly does *NOT* "push every member that matches the department selected description".

In fact, when it finds the matched description, all it does is break out of the loop and so the loop really accomplishes nothing whatsoever.

And why do you loop 200 times? What happens if the array only has 78 elements? Or 237 elements?

On top of that, you are pushing the SAME INFORMATION no matter what is in the PHP array.

Time to go ask in the PHP forum, methinks.

p.s.: This really is *NOT* the best way to do this, by the by. You really ought to have a JavaScript constructor for a Member object, for example.