Confusing results using associative javascript array

Discussion in 'JavaScript' started by papa_face, May 8, 2010.

  1. #1
    Hey,

    I am trying to get some information about an array I have created in JS.
    My code is:
    var passInfo = [];
    
    	
    	//ID3
    	passInfo['conv_Id3Title'] = document.getElementById('conv_Id3Title').value; 
    	passInfo['conv_Id3Album'] = document.getElementById('conv_Id3Album').value; 
    
    	alert(passInfo.length);
    Code (markup):
    When the alert pops up I get 0 displayed. Why is it saying that this array has no elements?
     
    papa_face, May 8, 2010 IP
  2. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #2
    Ok, I've sorted that out as it appears the length method doesnt work for associative arrays.
    My next confusing issue is:
    	var passInfo = [];
    
    	passInfo['conv_Added'] = document.getElementById('conv_Added').value; 
    	passInfo['conv_From'] = document.getElementById('conv_From').value; 
    
    	finaldata = JSON.stringify(passInfo);
    	alert(finaldata);
    Code (markup):
    I get an alert with "[]". Shouldn't this be returning the json string?
    and yes, the json object is definitely included in the page.
    	var passInfo = [];
    
    	passInfo[0] = document.getElementById('conv_Added').value; 
    	passInfo[1] = document.getElementById('conv_From').value; 
    
    	finaldata = JSON.stringify(passInfo);
    	alert(finaldata);
    Code (markup):
    Works without any problems
     
    papa_face, May 8, 2010 IP