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.

5
  • T1001 etc. are variables containg a number? If so, you have a very regular indexed array. Commented Jul 3, 2013 at 9: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. Commented Jul 3, 2013 at 9:42
  • @ Teemu.. Yes it contains numeric value..as the 1001,1003,1005 present in my question. Commented Jul 3, 2013 at 9: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. Commented Jul 3, 2013 at 9: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. ;) Commented Jul 3, 2013 at 10:01

2 Answers 2

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 );
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

2 Comments

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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.