Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array vs object

Status
Not open for further replies.

SarahKate31

Programmer
Feb 14, 2002
34
US
hi - so here is my issue:
on my first page, i am saving info into a 2 dimensional array which is stored in a hidden frame in my frameset. then i am accessing that array from my second page. if i try to access the array elements individually (array[1][2]) there is no problem. the information was stored and i can get to all of it. but when i tried to alert the entire array, the alert said: [object]. so for some reason i cannot reference the array as a whole, or even by the 1st dimension (array[0]), i can only reference it if i go to the very last level. the problem is that i want to sort the array and since it is not being recognized as an array but as an object, the .sort() is throwing an error. No other array methods will work on it either. Any idea why it is not being recognized as an array even though it clearly is since it does contain the data?

thanks - kate
 
Javascript is an Object-Oriented language, and arrays are objects. If you want to see the contents, you can use

arrayname.toString();

or

arrayname.valueOf();

The .sort() method will sort a single dimension of an array as long as the values are numbers or strings themselves. According to Microsoft's JScript help (which should be relevant here):

arrayobj.sort(sortfunction)

The sortfunction argument is the name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.

If you don't have an array of ASCII characters, it won't sort. The first dimension of a 2-dimensional array doesn't qualify to be sorted.

If you want to sort all the elements of the array in each dimension, you can use:

for (var ai=0;ai<arrayname.length;ai++)
{
arrayname[ai]=arrayname[ai].sort();
}

If you want the whole array sorted in ASCII order, you have to put it all in one dimension, though.

Lee
 
i understand that arrays are objects but i don't understand why i can't access this one. if i just declared a 2D array in a page and populated it and then did array.sort() it would still sort even though it is 2D. sorting is no problem with 2D arrays normally. but when i try to sort this 2D array, the error is saying "unexpected method" etc because it is not recognizing this object as an array object so array methods cannot be applied to it. what i dont understand is that it is clearly an array but i cannot apply array methods to it.
 
Since you didn't show your code, helping you any further would just be a guessing game.

Lee
 
i'm sorry if i have not provided enough information...it is a complicated problem and difficult for me to explain...here are the relevant sections of code:


Hframe is the hidden frame
main is the main viewable frame

in page1.html:

function populateArray(i){
ColorArray = new Array()
ColorArray[0] = i
ColorArray[1] = "Blue"
ColorArray[2] = "Green"
ColorArray[3] = "Yellow"
ColorArray[4] = "Purple"

len=window.parent.Hframe.COLORS.length
window.parent.Hframe.COLORS[len] = new Array()
window.parent.Hframe.COLORS[len] = ColorArray
}

function Go(){
for(i=0;i<5;i++) {
populateArray(i);
}
window.parent.main.location.href = "page2.html"
}


populateArray() is called by Go() from page1.html
then you are sent to page2.html

in page2.html:
onLoad this function is called:

function WritePage(){
var CArray = new Array ()
CArray = window.parent.Hframe.COLORS;

alert(CArray[0][1]);
alert(CArray);
CArray = CArray.sort();
}

the first alert displays what it should: "Blue"
the second alert displays as "[object]"
the line calling the sort function causes an error saying that it was an "unexpected method"

i would like to find a way to sort this array if possible...if i'm still not making any sense, then just forget it, and thank you for your time

--kate
 
Your sort won't work because the sort() method needs to know what to sort on. For non-object data types it's implied but, for example, if you wanted to sort the arrays based on the first position of each array, you could do this:
Code:
function WritePage(){
  var CArray = new Array ()
  CArray = window.parent.Hframe.COLORS;
  CArray = CArray.sort(mySort);
}

function mySort(a,b){
  [green]//Change this to whatever position you want to sort on[/green]
  [red]var pos = 0;[/red]
  if(a[pos] > b[pos]){return 1}
  if(a[pos] < b[pos]){return -1}
  return 0
}

Adam

Pedro offers you his protection
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top