www.webdeveloper.com
Results 1 to 8 of 8

Thread: Storing data into Array

  1. #1
    Join Date
    Dec 2003
    Posts
    11

    Question Storing data into Array

    I am trying to store data into an Array in JS. The following works:

    Code:
    arrayName = ['name','Owa22','Owa23','Owa24'];
    Then doing a arrayName[1] would print out the value Owa22

    But the problem is, I have the following:

    Code:
    var vehicleName = '['+searchLocation.getElementsByTagName("vehicleName")[0].childNodes[0].nodeValue+']';
    
    arrayName = vehicleName;
    And when I do a:
    arrayname[1], it will show me the character '

    How do I store a string of text into a JS's array, frm the above...?

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    You are quoting the brackets ']' thus creating a string as vehicleName's value and therefore arrayName's value. Take the quotes off the brackets and remove the + signs and then the string from the nodeValue will be in array.

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Can we see an example of the searchLocation.getElementsByTagName("vehicleName")[0].childNodes[0].nodeValue ? How does this string look like?

  4. #4
    Join Date
    Dec 2003
    Posts
    11
    The value of searchLocation.getElementsByTagName("vehicleName")[0].childNodes[0].nodeValue is:

    'name','owa','owb','owc'

    I tried removing the brackets and the + but it didnt work. Maybe the above text isn't a proper array?

    Also, I tried changing the above searchLocation.getElementsByTagName("vehicleName")[0].childNodes[0].nodeValue 's value to:

    ['name','owa','owb','owc']

    But it still didnt't work.... The above text are generated by Java, and sent to the web application through <vehicleName>['name','owa','owb','owc']</vehicleName>, and using the searchLocation.getElementsByTagName("vehicleName") would extract the data out, and will then be stored under the JS var vehicleName...

    Am I doing it the wrong way?

  5. #5
    Join Date
    Dec 2003
    Posts
    11
    Code fragments:

    Code:
    if (http.readyState == 4) {
                        if (http.status == 200) {
                                                    var searchLocation = http.responseXML.getElementsByTagName("searchLocation")[0];
                     
                            var results;
                           if (searchLocation!=null){
                                results = searchLocation.getElementsByTagName("latlng")[0].childNodes[0].nodeValue.split(",");
                                
    var vehicleName = searchLocation.getElementsByTagName("vehicleName")[0].childNodes[0].nodeValue;//searchLocation.getElementsByTagName("vehicleName")[0].childNodes[0].nodeValue
    
    //arrayName = ['name','Owa22','Owa23','Owa24'];
                                arrayName = vehicleName; //['name','Owa22','Owa23','Owa24'] <-- Problem here
                                alert('vehicleName '+vehicleName);
                                alert('arrayName[1] '+arrayName[1]);
                            }
    A sample XML that the above receives:

    <searchLocation>
    <latlng>29.01110,101.20021</latlng>
    <vehicleName>['name','owa','owb','owc']</vehicleName>
    </searchLocation>
    Last edited by mingteikg; 04-06-2009 at 10:47 AM.

  6. #6
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    whatever text you read from the DOM, javascript read it as a string type value. what you are expecting is to use that string and be treated as javascript statement. in that case you have to eval the string.
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

  7. #7
    Join Date
    Dec 2003
    Posts
    11
    Yep, eval() fixed the problem!

    Thanks!

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Well, eval() is to be avoided whenever is possible, and in this case it is possible. Write your XML like:
    Code:
    <searchLocation>
    <latlng>29.01110,101.20021</latlng>
    <vehicleName>name,owa,owb,owc</vehicleName>
    </searchLocation>
    Now you will achieve an array by simply splitting the string response upon the delimiter (the comma)
    Code:
    var vehicleName = searchLocation.getElementsByTagName("vehicleName")[0].childNodes[0].nodeValue;
    var arrayName= vehicleName.split(',');
    Last edited by Kor; 04-07-2009 at 02:34 AM.

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