473,473 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

for (var i in obj) question

Claus Mygind
571 Contributor
I have an array of objects.

I display the content of the array in a table.

Then I have a link which lets the user sort the table. I do that by sorting the array, delete the content of the table and then redisplay the content of the array. But the "for (var i in obj)" seems to use the original order. I can see in firebug that the array is properly sorted. So why does it maintain the original sort order?

Here is my sample code and below is a screen shot of the array after sorting;

Expand|Select|Wrap|Line Numbers
  1. function a7DayKeysSort()
  2. {
  3. /*
  4. -----------------------------------------
  5.     clear previous values
  6.         but preserve the header line
  7. -----------------------------------------
  8.     */
  9.     var tr, td
  10.     tbody = document.getElementById("dJobLines");
  11.     while (tbody.rows.length > 1) 
  12.     {
  13.         tbody.deleteRow(1);
  14.     }
  15. /*
  16. --------------------------------
  17.        sort content of array
  18. --------------------------------
  19. */
  20.     a7DayKeys.sort();
  21.  
  22. /*
  23. ----------------------------------------------
  24.        now re-display content of array
  25. ----------------------------------------------
  26. */
  27.     for ( var ix in a7DayKeys )
  28.     {
  29.         cKey = a7DayKeys[ix];
  30.  
  31.         tr = tbody.insertRow(tbody.rows.length);
  32.  
  33.         td = tr.insertCell(tr.cells.length);
  34.         td.setAttribute("align", "center");
  35.         td.innerHTML = cKey.pJob;
  36.  
  37.         td = tr.insertCell(tr.cells.length);
  38.         td.innerHTML = cKey.pName;
  39.  
  40.         td = tr.insertCell(tr.cells.length);
  41.         td.setAttribute("align", "center");
  42.         td.innerHTML = cKey.pPC;
  43.  
  44.         //load check boxes for true values
  45.         for (var nx = 0; nx < 7; nx++ )
  46.         {
  47.             td = tr.insertCell(tr.cells.length);
  48.             td.setAttribute("align", "center");
  49.             if ( cKey[nx] == true )
  50.             {
  51.                 td.innerHTML = '<INPUT TYPE="checkbox" NAME="">';
  52.             }else{
  53.                 td.innerHTML = "&nbsp;";
  54.             }
  55.         }
  56.     }
  57. }
  58.  

Sep 10 '09 #1
10 5113
Dormilich
8,658 Recognized Expert Moderator Expert
upon trying myself, sorting occured.
Sep 10 '09 #2
Claus Mygind
571 Contributor
I just added a picture of the array as viewed from firebug. The array is sorted but the "for ( var i in obj)" is using the original sort order. Why would this be?
Sep 10 '09 #3
Dormilich
8,658 Recognized Expert Moderator Expert
judging from the picture, a7DayKeys is not a standard array (besides that Firebug always display the properties and methods sorted)

out of interest, what does
Expand|Select|Wrap|Line Numbers
  1. typeof a7DayKeys
give?
Sep 10 '09 #4
acoder
16,027 Recognized Expert Moderator MVP
What exactly are you sorting on? Except for very simple alphabetical sorts, you will need to supply a sort function to sort() - see https://developer.mozilla.org/en/Cor...cts/Array/sort.
Sep 10 '09 #5
Claus Mygind
571 Contributor
@Dormilich
It returns that a7DayKeys is an "Object"
Sep 10 '09 #6
Claus Mygind
571 Contributor
@acoder

I just wanted the keys of the object/array to be displayed in a sorted order. As was previously pointed out the display I show in my picture is the order I wish the records to appear. The unique key is made up of "-" + .job +.pc

initially I left out the "_" from the key. When I did that I was able to sort, but the key was changed from the original value to a standard array 0, 1, 2 etc.

also the key value is not always a number as shown. Other values can start with a letter like P or L.

So I added the underscore to make all values strings. That's when my trouble started.

I have other sorts on the same page where I do employ the suggestion in the link you sent and they work fine. In those arrays I sort on one of the attribute values.

So what you are saying I need to create a function that compares the value of the attributes, or create an attribute with the same value of Job+Pc and use that to compare.

Expand|Select|Wrap|Line Numbers
  1. a7DayKeys.sort(stringAsort);
  2.  
  3. //sorts character fields ascending order
  4. function stringAsort(a, b) {
  5.     a = a[job].toLowerCase();
  6.     b = b[job].toLowerCase();
  7.     return ((a < b) ? -1 : ((a > b) ? 1 : 0));
  8. }
  9.  
Sep 10 '09 #7
Dormilich
8,658 Recognized Expert Moderator Expert
be aware that Array.sort() works on the values, not on the keys! (and because your values itself are objects again…)

in this regard you should refrain from terming your object as array (though it technically may be an array)
Sep 10 '09 #8
Claus Mygind
571 Contributor
@Dormilich

Ok I see what you are saying. We will call it an object. Can I sort the attributes of the object? So they are recalled in the order that is shown?

I stated that poorly.

Can I sort the list of objects in my object

Not sure if that makes it clearer
Sep 10 '09 #9
Dormilich
8,658 Recognized Expert Moderator Expert
can you make it a real array (depends on the usage) like
Expand|Select|Wrap|Line Numbers
  1. var new_entry_obj = new Entry([false, true, false, …], 65583, "OAK BRO", 60);
  2. // the Entry object has a toString() method,
  3. // which will return "712634" (or whatever)
  4. // so that you can apply Array.sort()
  5. a7DayKeys.push(new_entry_obj);
?

you could also ask the FireBug guys ;)
Sep 10 '09 #10
Claus Mygind
571 Contributor
Thanks for your reply. I think at this time I am going to drop the idea of the sort option. Looks like I will have to study arrays and objects some more.
Sep 10 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ralph Freshour | last post by:
I'm uploading a file to the server - I check the $_FIILES variable to check for exceeding a certain limit (250KB in this case) _ I notice that when I try to upload files of 500KB or 900KB holds...
5
by: Brian Angliss | last post by:
I'm relatively new to scripting in JavaScript, so I'm not too surprised I'm having difficulty scripting up an animation effect for my personal site. What I'm trying to do is the following: When...
8
by: Nick | last post by:
I have the following code: var obj = {a:0, b:1, ...} function f() {...} obj.f = f // This will make a instance method? How to make a Class method for the class of obj? Or what's the...
6
by: Jon Davis | last post by:
I recently learned how to do an <OBJECT> alternative to <IFRAME> in current browsers using: <object id="extendedhtml" type="text/html" data="otherpage.html" width="250" height="400"></object> ...
2
by: King Albert | last post by:
Question : Is it 'good design' to make an eventhandler a method of your class ? Problem in the eventhandler below : I need 'this' to refer to Tabel, not to the clicked element ! ...
3
by: Eric Layman | last post by:
Hi, I have a script here which will loop thru a table and check for it's background color. But it doesn't work on Firefox. The Error Inspector said "ERROR. obj.cells has no properties How...
5
by: msalman | last post by:
hey all, i'm very new to DOM and javascripting. I found a tutorial somewhere and i was playing around with it but it got stuck on getting the value of a node that i just append at the end. It...
3
by: nicemotion | last post by:
Hallo, i need to insert in the 'MarcaLinea_Pdf' field the value of the var 'recordID' sent from a page ( insert_pdf.php?recordID=14) it's easy, i know, but... Also, how to Printout the...
25
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.