比较数组中的第一个值与其他值

Compare first value in array with others

本文关键字:其他 第一个 数组 比较      更新时间:2023-09-26

如果数组中的第一个值比其他值高或低,建议使用什么方法进行比较

下面有一个数组

var a = [8,3,14,34,0,2]

我想通过js比较值a[0]是否高于或低于其他数组值。编辑:预期var a结果:8较小,因为数组中有比8更高的值。

示例2:var b = [34,2,23,8]预期输出:更高,因为所有其他数字都低于a[0]

如果你想知道它是比所有其他值都高还是比所有其他值都低,你可以像下面这样调用min和max函数

var min = Math.min.apply(null, a);
   var max = Math.max.apply(null, a);

最好的方法是从1开始的forloop。

for(i = 1; i < a.length;i++){
    if(a[0] > a[i])
    {
       //do something
    }
    else if(a[0] < a[i])
    {
       //do something
    }
}

严格相等性测试

var a = [8,3, 114,34,0,2];
a.forEach(function(element) {   
   element === a[0] ? console.log (element + ' is equal to ' + a[0]) :
   element > a[0] ? console.log(element + ' is higher than ' + a[0]) :
   console.log(element + " is lower than " + a[0]); 
});
//"8 is equal to 8"
//"3 is lower than 8"
//"114 is higher than 8"
//"34 is higher than 8"
//"0 is lower than 8"
//"2 is lower than 8"

// Create an array of -1/0/+1 for each value relative to first elt.
const comps = ([head, ...tail]) => tail.map(e => e < head ? -1 : e === head ? 0 : +1);
// Define some little convenience routines.
const greater = c => c === +1;
const less    = c => c === -1;
// See if some or all element(s) are greater or less.
const someGreater = a => comps(a).some(greater);
const someLess    = a => comps(a).some(less);
const allGreater  = a => comps(a).every(greater);
const allLess     = a => comps(a).every(less);
// Test.
const input = [8,3, 114,34,0,2];
console.log("Some are greater", someGreater(input));
console.log("Some are less", someLess(input));
console.log("All are greater", allGreater(input));
console.log("All are less", allLess(input));

一个有趣的把戏:

function first_is_bigger (array) {
  var comp = array.join(" && " + array[0] + " > ");
  return Function("return 1 | " + comp + ";")();
}
first_is_bigger([0, 1, 2]) // false
first_is_bigger([0, -1, -2]) // true

解释:

array = [1, 2, 3];
comp = array.join(" && " + array[0] + " > ");
// comp = "1 && 1 > 2 && 1 > 3"
exec_comp = Function("return " + comp + ";");
// exec_comp = function () { return 1 && 1 > 2 && 1 > 3; }
exec_comp()
// false

问题:0 && anything总是false:

array = [0, -1, -2]
comp = array.join(" && " + array[0] + " > ");
// comp = "0 && 0 > -1 && 0 > -2"
exec_comp = Function("return " + comp + ";");
// exec_comp = function () { return 0 && 0 > -1 && 0 > -2; }
exec_comp()
// false :-(

修复:1 | anything总是不同于0:

exec_comp = Function("return 1 | " + comp + ";");
// exec_comp = function () { return 1 | 0 && 0 > -1 && 0 > -2; }
exec_comp()
// true :-)

警告:不正确使用动态求值会使代码容易受到注入攻击:-(

)

根据我对这个问题的理解,我们打算检查给定的元素是否是数组的最大值(在我们的具体情况中是第一个)。我已经实现了一个更通用的function,在那里你可以检查任何元素,但index 0是默认的。

function isHigher(input, index) {
    if (index === undefined) {
        index = 0;
    }
    for (var i in input) {
        if ((i !== index) && (input[i] > input[index])) {
            return false;
        }
    }
    return true;
}

调用isHigher(a)检查第0个元素是否最大。如果您想检查第五个元素,调用isHigher(a, 5)