0

我有一个这样的数组:

[1, Stopped]
[2, Waiting]
[3, Finished]
[4, Stopped]
[5, Running]

数字是程序的id,文本是程序的状态。我需要根据下一个顺序对该数组进行排序:

['Error','Halted','Blocked','Finished','Waiting to Start','Waiting','Stopping','Running','Idle','Stopped','Opened','Ready'];

我将它用于除 IE8 之外的所有其他浏览器:

var a = [[1, 'Stopped'],
[2, 'Waiting'],
[3, 'Finished'],
[4, 'Stopped'],
[5, 'Running']];
var order = ['Error','Halted','Blocked','Finished','Waiting to Start','Waiting','Stopping','Running','Idle','Stopped','Opened','Ready'];

a.sort(function (a, b) {
   return order.indexOf(a[1]) - order.indexOf(b[1]);
});

它适用于除 IE8 之外的所有不同浏览器。谁能告诉我如何在 IE8 中对其进行排序?

4

1 回答 1

2

IE8 does not support array.indexOf(). You'll need to find an alternative.

Possible solutions:

  1. Use jQuery (or a similar library), which gives $.inArray() as a direct replacement for array.indexOf().

  2. Use a polyfill library that adds a replacement .indexOf method to the Array prototype. Here's one possible option. (others available via google of course)

  3. Rewrite your code to search through the array using a simple loop.

于 2013-05-30T08:32:32.620 回答