Reputation: 51
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
Upvotes: 4
Views: 486
Reputation: 77089
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]);
}
Upvotes: 0
Reputation: 122906
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.
Upvotes: 1
Reputation: 95252
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").
Upvotes: 6
Reputation: 77089
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]);
}
Upvotes: 2