0

Is it possible to have an Associate Array / Key Pair Array arranged in with random order of keys. Whenever, I am adding a new element, javascript is automatically sorting the keys.

For example,

When I'm creating an array as

a[T1001] ="B"
a[T1005] = "A"
a[T1003] ="C"

and later the array list is verified, I'm seeing that the array is automatically ordered as :

a[T1001]
a[T1003]
a[T1005] 

Is it possible to retain the same order in which I am assigning the values to the array ?. I cannot use the Push method.. It creates an indexed array with the individual objects added as its values.

  • `T1001` etc. are variables containg a number? If so, you have a very regular indexed array. – Teemu Jul 03 '13 at 09:41
  • 2
    There is no such thing as an `Associate Array` in javascript. And the order of object-properties is undefined and thus not dependable. – Yoshi Jul 03 '13 at 09:42
  • @ Teemu.. Yes it contains numeric value..as the 1001,1003,1005 present in my question. – Rithesh Krishnan Jul 03 '13 at 09:53
  • @ Yoshi, Yes. I am aware of that. But, my requirement is such that i need to address each element by using a specific key. – Rithesh Krishnan Jul 03 '13 at 09:54
  • @RitheshKrishnan Then you'll probably need to create your own data-structure which can handle such requirements. Simply having a requirement won't change how js works. ;) – Yoshi Jul 03 '13 at 10:01

2 Answers2

3

Objects in JavaScript (and your "array" is just an object here) have no inherent order of their properties you can rely on.

As a workaround, you could store the order of keys in a separate array:

var order = [];

a['T1001'] = "B";
order.push( 'T1001' );
a['T1005'] = "A";
order.push( 'T1005' );
a['T1003'] = "C";
order.push( 'T1003' );

If you then traverse the order array, you can get your required order:

for( var i=0; i<order.length; i++ ) {
  console.log( a[ order[i] ] );
}

EDIT

Removing elements from the first list would require the use of indexOf and splice():

function delElement( arr, order, index ) {
  // get the position of the element within the order
  var position = order.indexOf( key );

  // delete element from list
  delete arr[ order[ pos ] ];

  // remove from ordering array
  order.splice( pos, 1 );
}
Sirko
  • 72,589
  • 19
  • 149
  • 183
  • Thanks Sirko for your reply. The resultant list, which i am trying to use is like an associated array. for example .. delete an item using "delete a[t10001]. This would remove the item from the array. When i use the Push, the Key which i have set as the index would be gone. – Rithesh Krishnan Jul 03 '13 at 09:52
0

Looks to me as if your using an object. The sorting of an object's properties is never guaranteed. I don't think internet explorer (at least the old versions) sorts the properties but chrome does by default.

Your code is invalid, it should have the properties as strings if they are not numbers. And you would have to use an object

a = {};
a["T1001"] ="B"
a["T1005"] = "A"
a["T1003"] ="C"

You can either use two arrays to store your properties and the value if you want to preserve the ordering, or you cannot guarantee the ordering if you insist on using an object.

Two arrays

a1 = []; a2 = []
a1[0] = "T1001"; a2[0] = "B";
a1[1] = "T1005"; a2[1] = "A";
a1[2] = "T1003"; a2[2] = "C"; 

EDIT: Unless I guessed at what you meant wrong and that T1001, etc are variables containing numbers.

Ally
  • 4,894
  • 8
  • 37
  • 45
  • Ally, sorry for the errors in the code. Yes you are right, about the change.. I think the solution what you have provided is worth to attempt. Will reply in a short while with the results. – Rithesh Krishnan Jul 03 '13 at 09:56
  • Hello Ally, Thanks for the suggestion... I used it... however with a slight difference. - I added the order array part of the first array itself. This helped in traversing through the array, and also the surety that the list order is easily accessible. - I created the first array as below : var masterArray = new Array(); var masterArray.KeyOrder = new Array(); Then, i stored the list of values in the master Array, and whenever i need to loop through, i used the sub-array "KeyOrder". Addl.Reference [link](http://stackoverflow.com/questions/2798893/ordered-hash-in-javascript) – Rithesh Krishnan Jul 04 '13 at 09:23