### 从阵列中获取具有最高优先级的字符串的最有效方法

#### [英]Most efficient way to get the string with highest priority out of an array

Let's say I have an array as follow:

``````var numArray = [ 1, 2, 1, 2, 1, 1, 3, 2, 1, 3 ];
``````

I could easily get the maximum value out of it by using:

``````Math.max.apply(Math, array);
``````

But what if I had an array as follow:

``````var stringArray = [ "Foo", "Very Foo", "Not So Foo",
"Foo", "Very Foo", "Extremely Foo" ];
``````

Then what would be the most efficient way to get the maximum value out of it, maximum value being, in order of importance: "Extremely Foo" > "Very Foo" > "Foo" > "Not So Foo"

The first thing that came to mind was iterating through the array with a loop and comparing manually as such:

``````var maxValue = "";
for(var i = 0; i < stringArray.length; i++) {
if(maxValue != "Extremely Foo") {
if(maxValue != "Very Foo") {
if(maxValue != "Foo") {
maxValue = stringArray[i];
}
else if(stringArray[i] != "Not So Foo") {
maxValue = stringArray[i];
}
}
else if(stringArray[i] == "Extremely Foo") {
maxValue = stringArray[i];
break;
}
}
else { break; }
}
``````

Then I thought about making an enum to give each different string a value:

``````enum Foos {
Not So Foo = 0,
Foo = 1,
Very Foo = 2,
Extremely Foo = 3,
}
``````

And using the enum value as a comparison method to use the greater than logical operator instead.

My question is: What's the best way to achieve what I am trying to achieve? One of the two ways I mentioned or is there a different way to do it in Javascript that is more efficient?

## 5 个解决方案

### #1

1

You can use Array#reduce, and on each iteration take string with the highest value in the enum:

``````var foosEnum = { "Not So Foo": 0, "Foo": 1, "Very Foo": 2, "Extremely Foo": 3 };
var stringArray = [ "Foo", "Very Foo", "Not So Foo", "Foo", "Very Foo", "Extremely Foo" ];

var result = stringArray.reduce(function(c, n) {
return foosEnum[c] > foosEnum[n] ? c : n;
});

console.log(result);``````

### #2

1

If you had an array of `fooportance`, you could create a `fooness` function that determines the most foo value in the array of foos

``````function fooness(foo) {
var fooportance = ["Extremely Foo", "Very Foo", "Foo", "Not So Foo"];
return fooportance.indexOf(foo);
}

var stringArray = ["Foo", "Very Foo", "Not So Foo", "Foo", "Very Foo", "Extremely Foo"];

stringArray.sort(function(a, b) {
return fooness(a) > fooness(b);
});

var mostFoo = stringArray[0];

console.log(mostFoo)``````

### #3

0

You could make an array of your priorities in ascending order, then sort your `stringArray` using the order index:

``````const stringArray = [ "Foo", "Very Foo", "Not So Foo",
"Foo", "Very Foo", "Extremely Foo" ]
const order = ['Not so Foo', 'Foo', 'Very Foo', 'Extremely Foo'];
stringArray.sort((a, b) => order.indexOf(b) - order.indexOf(a))
console.log(stringArray)``````

### #4

0

You should have an array of numbers and simply convert the numbers into strings if you need to display them.

### #5

0

You can map your keys to numbers (you've got four keys so : 0,1,2,3), then sort the strings using the numbers, after that the highest string will be at the first position (0) :

``````var myMap = {"Not So Foo":0,
"Foo":1,
"Very Foo":2,
"Extremely Foo":3};
var stringArray = [ "Foo", "Very Foo", "Not So Foo",
"Foo", "Very Foo", "Extremely Foo" ];
stringArray.sort(function(a,b){
return myMap[b] - myMap[a];
});
var highest = stringArray[0];
``````