0
如果我有類型的數組...排序陣列,無論位置和string [ES6]
const types = ['train','bus','car'];
// needs to re-ordered to represent the order belonging to transportationFields.
和領域的數組...
const transportationFields = [
'bus_station', 'bus_stop', 'bus_line', 'took_bus' // bus -> 1st in the order
'car_type', 'buyer_of_car', 'car_model', // car -> 2nd
'train_number', 'train_trip_num', 'train_stop', // train -> 3rd
];
我我想重新排序types數組以匹配transportationFields數組的順序。我能想到使這項工作的唯一方法是基於字符串過濾,然後排序...
預期輸出:const newTypes = ['bus','car','train'];
我已經試過這樣:
const newTypes = transportationFields.filter((pos) => types.indexOf(pos) > -1);
但是,從字面上看看確切的字符串名稱。如何根據運輸字段順序中是否存在類型字符串來排序?提前謝謝你的幫助。 (使用陣營ES6)
嗯仍返回[ '火車', '公交車', '汽車']我。我可能無法正確轉換爲es6 – Modelesq
const只是一種初始化變量的方法。類似於「var a = 1;」 – Modelesq
嗯,對我來說,在Chrome瀏覽器上工作正常嗎? – juvian