将对象数组重新排序为特定顺序

时间:2016-07-22 09:57:25

标签: javascript arrays

我有这样的数组

var arr = ["hub","master","hub","transit","transit","master"];

我想将上面的数组重新排序为特定的顺序,如

var arr = ["master","hub","hub","master","transit","transit"];

例如,如果我得到像

这样的数组
Ex1:
var a = ["hub","transit","master","master"];
result = ["master","hub","master","transit"];

Ex2:
var a = ["hub","master"];
result = ["master","hub"];

我知道使用for循环检查项目在哪个位置并推入新数组但我想使用更优化的方式。

任何想法......谢谢

0 个答案:

没有答案