Jump to content

Problem with Arrays


Mercedes

Recommended Posts

Hi.I’m really a beginner with javascript and I don’t know where to find real help.I simplify my problem to show you what it is, in the script I’m sending you: <html> <body> <script type="text/javascript">var x; var s=0; var p=0; var myarray=new Array(); var MultiArray=new Array(0,0); var mycars = new Array(); mycars[0] = "Saab";mycars[1] = "Volvo"; mycars[2] = "BMW"; var Owner = new Array(); Owner[0] = "Mercedes"; Owner[1] = "Vittorio"; Owner[2] = "Adriano"; for (s=0;s<=2;s++) { myarray[0]=mycars; myarray[1]=Owner; MultiArray = myarray; document.write('s = ' + s + ' and elements are: ' + MultiArray + "<br />"); // Here I get det correct values. } document.write("<br />"); document.write("<br />");for (p = 0; p<MultiArray.length; p++) { document.write(' p = ' + p + ' and elements are: ' + MultiArray[p]+ "<br />"); } document.write("<br />"); document.write("<br />"); for (x in MultiArray) { document.write(' x = ' + x + ' and elements are: ' + MultiArray[x][0] + " AND "); document.write(MultiArray[x][1] + "<br />"); } </script> </body> </html>What am I doing wrong?. Why can’t I get all the values in the respective element???.Thank you for your help.Mercedes

Link to comment
Share on other sites

for (p = 0; p<MultiArray.length; p++) {  document.write(' p = ' + p + ' and elements are: ' + MultiArray[p][0]+ ", " + MultiArray[p][1] + "<br />"); }for (x in MultiArray) {  if ((xtypeof x) != 'function')  {	document.write(' x = ' + x);	document.write(' and elements are: ' + MultiArray[x][0] + " AND ");	document.write(MultiArray[x][1] + "<br />");   }}

When you use for..in you iterate through everything in the object, not just array elements. Javascript uses soft objects so arrays and objects are pretty much the same thing. When you use for..in you will also get all the array methods, so you will get array.push, array.pop, array.sort, etc. The if statement in the for loop makes sure that the element is not a function. That still doesn't guarantee it's not a property though (like array.length), so it's best with arrays to use a regular for loop to access the elements.

Link to comment
Share on other sites

Hi!.Thank you for answering my question. (both of you)But I maybe have not been clear enough.I changed the for in as you said, justsomeguy, but it didn’t helpMy problem is the outcome. I need to use arrays of arrays because in my real script I have different arrays that I need to present as 1 at the end.I’ try to explain better:I want to have the output as the one I get in the first loop (with s) where I put some values in the array. I checked if the right values were put in the right element of the other array, and it is OK. I get:s = 0 and elements are: Saab,Mercedess = 1 and elements are: Volvo,Vittorios = 2 and elements are: BMW,AdrianoBUT, when I try to see what are the values in the elements, Outside the first loop, I get this output:p = 0 and elements are: BMW, Adrianop = 1 and elements are: BMW, Adrianop = 2 and elements are: BMW, AdrianoAnd with for in this:x = 0 and elements are: BMW AND Adrianox = 1 and elements are: BMW AND Adrianox = 2 and elements are: BMW AND AdrianoIt seems as I got the last value of the first loop in all the elements of the last array.WHY????????????Thank you for your help again.Mercedes

Link to comment
Share on other sites

I believe the problem is here:

for (s=0;s<=2;s++){myarray[0]=mycars[s];myarray[1]=Owner[s];MultiArray[s] = myarray;document.write('s = ' + s + ' and elements are: ' + MultiArray[s] + "<br />"); // Here I get det correct values.}

Try changing that code to this:

for (s=0;s<=2;s++){myarray = new Array();myarray[0]=mycars[s];myarray[1]=Owner[s];MultiArray[s] = myarray;document.write('s = ' + s + ' and elements are: ' + MultiArray[s] + "<br />"); // Here I get det correct values.}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...