您可以使用对象数组:
var items = [
{id:10, producer:'Ford'},
{id:30, producer:'Rover'},
{id:11, producer:'BMW'},
{id:1, producer:'Fiat'},
{id:4, producer:'Renault'},
{id:2, producer:'Mitsubishi'},
]
// or, starting from your two arrays:
var items = [];
for (var i=0; i<ids.length && i<producers.length; i++)
items[i] = {id:ids[i], producer:producers[i]};
然后您可以按id对该数组进行排序:
items.sort(function(a, b){ return a.id-b.id; });
...并使用 for 循环对其进行迭代。
另一种解决方案是迭代器数组,以weirdo
正确的顺序循环遍历 id<->producer 对象 ():
var weirdo = {"10":"Ford","30":"Rover",...};
var ids = Object.keys(weirdo); // if not already built somewhere
ids.sort(function(a,b){return a-b;}); // sort numeric
// loop:
for (var i=0; i<ids.length; i++) {
var id=ids[i], producer=weirdo[id];
...
}