Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How do I push new values to the following javascript array?

    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.

  • #2
    New Coder
    Join Date
    Jul 2012
    Location
    Ukraine
    Posts
    71
    Thanks
    1
    Thanked 18 Times in 17 Posts
    It's a generic object, not an array. You can add a property to an object using the following statement:
    Code:
    json.coolness="34.33";
    //or
    json["coolness"]="34.33";

  • Users who have thanked oneguy for this post:

    sirsaula (07-18-2012)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    I'd advisde against callingyour object json as it would be too easy to get it confused with the JSON object built into JavaScript.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Jun 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

    PHP Code:
    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/ "}]; 

  • #5
    New Coder
    Join Date
    Jun 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Nevermind thanks guys figured it out!

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

    It is done this way

    PHP Code:
    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/"}); 

  • #6
    New Coder
    Join Date
    Jun 2012
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

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

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,193
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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