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

[英]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"

那么什么是最有效的方法来获得最大值,最大值是,按重要性顺序:“极端Foo”>“非常Foo”>“Foo”>“不那么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?

我的问题是:实现我想要达到的目标的最佳途径是什么?我提到的两种方式之一,或者在Javascript中使用更有效的方法吗?

5 个解决方案

#1


1  

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

您可以使用Array#reduce,并在每次迭代时获取枚举中具有最高值的字符串:

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

如果你有一个fooportance数组,你可以创建一个fooness函数来确定foos数组中最大的foo值

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:

您可以按升序排列优先级数组,然后使用订单索引对stringArray进行排序:

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) :

您可以将键映射到数字(您有四个键,因此:0,1,2,3),然后使用数字对字符串进行排序,之后最高字符串将位于第一个位置(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];
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2017/08/08/57930d0cf95155402116ed4f69cfd181.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告