4
Fruit={4:"apple",2:"banana",3:"graps"}; 
var k;
for(k in Fruit)
{alert(k);}

alert will be 2 3 4 in sequence in Ie9 when In IE8 & FF & other browse give 4 2 3 help me out of this. I need sequence is 4 2 3

binarious
  • 4,568
  • 26
  • 35
Ankit Thakkar
  • 51
  • 1
  • 6
  • check this [fiddle](http://jsfiddle.net/pABaQ/) - Chrome also does `2 3 4` – Tobias Krogh Apr 24 '12 at 11:21
  • 3
    for/in order is implementation specific; http://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop, you would need some sorting http://stackoverflow.com/questions/5467129/sort-javascript-object-by-key – Alex K. Apr 24 '12 at 11:24

4 Answers4

6

You cannot control the order that keys come out of an object in JavaScript. If you care about the order, put the keys in a separate array:

var fruit = { 4:"apple", 2:"banana", 3:"grapes" };
var fruitKeys = [ 4, 2, 3 ]
for (var i=0, l=fruitKeys.length; i<l; ++i) {
  var k = fruitKeys[i];
  alert(k + ":" + fruit[k]);
}

(as a point of style, you also shouldn't capitalize variable names; use capitals only for functions that you're using as constructors/"classes").

fiddle

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • If you're going to store the `fruit_keys` in another array anyway, why not make `fruit` a sparse array instead of an object? – kojiro Apr 24 '12 at 11:33
  • @kojiro: it depends on what they want out of the data. I assumed the choice of object was intentional and they need to look up fruit names by id at some point, but that is just an assumption. – Mark Reed Apr 24 '12 at 11:34
  • @MarkReed sure, but the ability to look up fruit by id wouldn't be diminished by using a sparse array, and you'd have an object with some additional potentially useful properties and methods that a simple object with numeric *property names* does not have. – kojiro Apr 24 '12 at 11:38
  • Ah, sorry, you did say sparse array. I thought you were referring to your array of pairs solution. You should post the sparse array solution and see if the OP likes it. It certainly works when the keys are small integers, as here, though it doesn't generalize very well. – Mark Reed Apr 24 '12 at 11:40
  • @MarkReed agreed, but generalization is out of the question anyway when you have to manually store `fruitKeys` in its own array. – kojiro Apr 24 '12 at 14:08
  • @kojiro: storing the keys in a separate array is a perfectly general way to preserve order. It's not terribly efficient, but it's very general. – Mark Reed Apr 24 '12 at 14:10
  • @MarkReed OK, I've added a cw answer that shows the sparse array approach. I don't understand how it's any less general than the approach you've given here, though. If you're so inclined, perhaps you could comment on it. – kojiro Apr 24 '12 at 14:22
  • @kojiro: all I meant is that the sparse array approach won't work if the keys aren't small integers. You have to use an object then. – Mark Reed Apr 24 '12 at 14:33
2

Associative arrays don't have order, so IE9's behavior here is not incorrect. There is no existing data type that can store the order this way. Your best bet would be to store the order explicitly:

var Fruit = [[4, "apple"], [2, "banana"], [3, "graps"]];
for (var i = 0; i < Fruit.length; i++) {
    alert(Fruit[i][0]);
}
kojiro
  • 74,557
  • 19
  • 143
  • 201
  • This is a good solution for the loop, but makes it awkward if you actually need to look things up by id in the object. – Mark Reed Apr 24 '12 at 11:31
  • Arguing about the usefulness of this approach doesn't mean much without context about the original question. If the asker wants a more useful answer, he should feel free to ask a more illustrative question. – kojiro Apr 24 '12 at 11:32
1

From the other answers it should be clear that javascript objects aren't ordered. You could use Object.keys in some browsers to do some kind of sorting. For example:

function objSortedList(obj){
  var keys = Object.keys(obj).sort(), key;
  while(key = keys.shift()){
      console.log(key+': '+obj[key]);
  }
}
//usage
var fruit = {4:"apple",2:"banana",3:"graps"};
objSortedList(fruit);
/* =>result:
  2: banana
  3: graps
  4: apple
*/

If you want to keep a predefined order, you could use the same slightly altered function:

function objSortedList(obj,keys){
  keys = keys || Object.keys(obj).sort()
  var key;
  while(key = keys.shift()){
   console.log(key+': '+obj[key]);
  }
}
//usage
var fruit = {4:"apple",2:"banana",3:"graps"};
objSortedList(fruit,[4,2,3]);
/* =>result:
  4: apple
  2: banana
  3: graps
*/

Another idea may be to store the order as a property of the Object and use that:

function objSortedList(obj,keys){
  keys = keys 
         || (obj.order ? obj.order.slice() : false) 
         || Object.keys(obj).sort()
  var key;
  while(key = keys.shift()){
   if (/^order$/.test(key)){ continue; }
   console.log(key+': '+obj[key]);
  }
}
//usage
var fruit = {4:"apple",2:"banana",3:"graps",order:[4,3,2]};
objSortedList(fruit);
/* =>result:
  4: apple
  3: graps
  2: banana
*/

For browsers that don't support Object.keys Mozilla offers a shim here. IE9 supports it.

KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • But what the OP wants is explicitly *not* sorted order, but insertion order. – Mark Reed Apr 24 '12 at 11:42
  • Hi. Thanks 2 all who tried 2 find out solution. i need more accurate answer as per my need . my array come from another file and javascript code in another file. and array dynamically set means i have more then one array of same name.. like fruit = {4:"apple",2:"banana",3:"graps"}; fruit = {4:"apple",3:"graps"}; fruit = {2:"apple",3:"graps"}; so i can't add any extra parameter to array give me any solution which is work in javascript. – Ankit Thakkar Apr 24 '12 at 13:39
  • If you are getting an object that's already defined by another process, the keys do not have any order. If you have JSON for the object and want to parse it yourself to find out what order the keys show up in, you can do that. But once it's a Javascript object, the order of the keys is gone forever, because objects are defined that way. Keys are not ordered. – Mark Reed Apr 24 '12 at 13:56
  • @AnkitThakkar Please revise your original question to include *all* the information you have available about how you want it answered. It's not fair to the people trying to help you to make them guess about anything you already know. – kojiro Apr 24 '12 at 14:10
0

Making this a community wiki because 1) asker has added criteria to his question and 2) this answer is essentially a modified version of Mark Reed's answer.

Another approach (a modified version of Mark Reed's answer) would be to use a sparse array. A sparse array is an array that doesn't have an element at every index within its length. If we take Mark's code, but convert fruit into an associative array, it looks like this:

var fruit = new Array();
fruit[2] = "banana";
fruit[3] = "graps";
fruit[4] = "apple";
// The rest of the solution is the same as Mark's:
var fruitKeys = [ 4, 2, 3 ]
for (var i=0, l=fruitKeys.length; i<l; ++i) {
  var k = fruitKeys[i];
  alert(k + ":" + fruit[k]);
}
Community
  • 1
  • 1
kojiro
  • 74,557
  • 19
  • 143
  • 201