开发者

How to disable the array auto sort in ie9

开发者 https://www.devze.com 2023-04-12 11:24 出处:网络
I have a javascript array(ary) and I need to print out each element as a select option in that array.

I have a javascript array(ary) and I need to print out each element as a select option in that array.

I found before the code goes into the loop - for (var i in ary), the array ary has been auto sorted in IE9, but on other browers, this doesn't happen. I don't want my array to be sorted in IE9, is there any way to disable the auto sort in IE9?

The ary array is an object-filled array which is converted from php array to javascript array. I don't quite familar with javascript, but I tried to use for(var i=0;i 开发者_如何学C

in the for(in) loop, I print out each element by using document.write(i) and I can see that in ie9 and chrome, the result is sorted by in firefox, the result is not sorted(which is what I want).

Following is the code

for (var i in ary) {
        selected = false;
        if (optional && !blank && (ary[i][valuef] == '')) {
            blank = true;
        }
        if (typeof(value) == 'object') {
            for (var j in value) {
                if (optional && !blank && (value[j] == '')) {
                    blank_selected = true;
                }
                if (ary[i][valuef] == value[j]) {
                    selected = true;
                    break;
                }
            }
        } else if (ary[i][valuef] == value) {
            selected = true;
        }
        if (selected) {
            output += '<option value="' + ary[i][valuef] +'" selected="selected">' + ary[i][namef] + '</option>';
        } else {
            output += '<option value="' + ary[i][valuef] +'">' + ary[i][namef] + '</option>';
        }
    }
    if (optional && !blank) {
        if (blank_selected) {
            output = '<option value="" selected></option>' + output;
        } else {
            output = '<option value=""></option>' + output;
        }
    }
    return output;
}


Use a for loop to iterate over arrays, not a for ( in ).

for (var i = 0, length = arr.length; i < length; i++) { ... }

The latter is for iterating over Object properties. When iterating over an Array with for (in) the order is implementation specific (as Object properties don't have a defined order), which doesn't make sense when iterating over an Array.


Arrays are just objects with a special length property and some handy methods. The indexes of an array are just properties with a numeric name. If you use for..in to iterate over the properties, you will get all enumerable properties, not just the numeric ones, and they will be returned in implementation dependent order.

Some browsers may return properties in alphabetical order, others in numeric order, others may return them in the order they were added. Some will preserve the order if properties are deleted and re-added, others will change the order.

The only way to get consistency is to access the numeric properties using a counter, such as:

var i, length = array.length;
for (i = 0; i < length; i++) {
  // do something with array[i]
}


Arrays do not sort themselves, but when you pull data out of an array in IE9 using the for (xx in yy) method, IE9 gives you the keys in sorted order. All other browsers (including IE8) give you the keys in the order in which you inserted them into the array.

So the workaround, as was mentioned above, to do this instead:

for (var i=0; i < arrayName.length; i++) {
  <do something>
}

This will pull the elements out of the array in the order in which you inserted them.


I'll throw an alternative solution into the mix. It is, unapologetically, a huge ugly hack, but gets around a problem.

If you have an array called 'theArray' like this

100 => 'a'
200 => 'b'
300 => 'c'

then replacing

for (key in theArray)

with

for (var i=0; i < theArray.length; i++)

does not really help, because the length is '3', but you have keys much higher than the array length. Presumably the keys in such instances have significance, and you want to keep them.

The hackish way to fix it is to force JavaScript to treat the keys as strings, not integers. You can do this by doing something quick and dirty like renaming all your keys to

"a100" => 'a'
"a200" => 'b'
"a300" => 'c'

Then use some sort of substring function when you want to use the key value later on in your code.

Once the keys are treated as strings, all browsers I am aware of will display them in the order they were created, not in any other order.

I should add a disclaimer - it's probably better to write your code to avoid this scenario in the first place by not using keys as a means to carry information in this way.

eg:

0 => array (100,'a')
1 => array (200,'b')

etc

This hack is something I've employed as a quick fix where I didn't want to rewrite a lot of code to not use 'meaningful' integers as keys.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号