www.webdeveloper.com
Results 1 to 10 of 10

Thread: How do I push new values to the following array?

  1. #1
    Join Date
    Jun 2012
    Posts
    18

    How do I push new values to the following 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. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380

    Lightbulb

    Don't need to push. Try this...
    Code:
    json['cool'] = '34.33';
    json['alsocool'] = '45454';

  3. #3
    Join Date
    Jul 2007
    Posts
    386
    JMRKER's example is to replace values.

    To add new ones just do like the example provided, but with new indexes.

    Code:
    json['newIndex'] = 'newValue';
    And because JS is cool, you can also do
    Code:
    json.newIndex = 'newValue'

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380

    Arrow

    Quote Originally Posted by SparoHawk View Post
    JMRKER's example is to replace values.

    To add new ones just do like the example provided, but with new indexes.

    Code:
    json['newIndex'] = 'newValue';
    It's the same syntax to replace or create new elements of the array.

    To display contents
    Code:
    For (var index in json) {
      alert(index+' : '+json[index]);
    }

  5. #5
    Join Date
    Jun 2012
    Posts
    18
    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@ttasdsau.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/ "}]; 

  6. #6
    Join Date
    Jul 2007
    Posts
    386
    You'd do:

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

  7. #7
    Join Date
    Jun 2012
    Posts
    18
    Dude I'm i allowed to tell you I love you????

    Thanks!!!!!!!!!!!!!

  8. #8
    Join Date
    Jul 2007
    Posts
    386
    You are most welcome. ^^

  9. #9
    Join Date
    Jun 2012
    Posts
    18
    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++;
                }
                
            }
            
        
    ?>

  10. #10
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,380

    Lightbulb

    This does not answer your last question, but it may provide food for thought ...
    Code:
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    
    var contacts = [ {  // literal assignment
      "LNAME":"Saula","FNAME":"Sir","MIDDLE":"","TITLE":"Ph.D.",
      "JOB":"Academic Dean of Engineering","SECTION":"Dean's Office",
      "EMAIL":"sirsaulajr@ttasdsau.edu","IMG":"/coe/dean/"
     },
     {
      "LNAME":"Student","FNAME":"John","MIDDLE":"Q.","TITLE":"Ph.D.",
      "JOB":"Associate Dean","SECTION":"Dean's Office",
      "EMAIL":"basdas@masma.com","IMG":"/coe/dean/images/"
     }
    ];
    
    contacts.push( {  // active literal assignment
      "LNAME":"Student","FNAME":"Mary","MIDDLE":"K.","TITLE":"MS.",
      "JOB":"Senior Associate Dean","SECTION":"Dean's Office",
      "EMAIL":"basdas@masma.com","IMG":"/coe/dean/images/"
     }
    );
    
    function addToContacts(newString) {  // dynamic assignment
      var tarr = newString.split(',');
      contacts.push( {
       "LNAME":tarr[0],"FNAME":tarr[1],"MIDDLE":tarr[2],"TITLE":tarr[3],
       "JOB":tarr[4],"SECTION":tarr[5],"EMAIL":tarr[6],"IMG":tarr[7]
      } );
    }
    
    function currentContacts() {  // display json array
      var str = '';
      for (var i=0; i<contacts.length; i++) {
        str += '#'+(i+1)+' '+contacts[i].FNAME+' '+contacts[i].LNAME+'\n'+contacts[i].JOB+'\n\n';
      }
      alert(str);
    }
    
    currentContacts();
    // following information might come from an ajax text file
    addToContacts('LName,FName,MI,Title,Job,Faculty,Email,Picture');
    currentContacts();
    
    </script>
    </head>
    <body>
    
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles