如果数组中的第一个值高于或低于其他值,推荐使用哪种方法进行比较。将数组中的第一个值与其他值进行比较
我有一个数组如下面
VAR一个= [8,3,114,34,0,2]
我想比较,如果值A [0]是高于或低于其他数组值通过js。 编辑:预计变量的结果:8是较小的,因为有更高的值比数组中的8。
出2:VAR B = [34,2,23,8] 预期输出:较高,因为所有其它的数字比一个[0]
如果数组中的第一个值高于或低于其他值,推荐使用哪种方法进行比较。将数组中的第一个值与其他值进行比较
我有一个数组如下面
VAR一个= [8,3,114,34,0,2]
我想比较,如果值A [0]是高于或低于其他数组值通过js。 编辑:预计变量的结果:8是较小的,因为有更高的值比数组中的8。
出2:VAR B = [34,2,23,8] 预期输出:较高,因为所有其它的数字比一个[0]
的最佳方式下将在1开始一个for循环。
for(i = 1; i < a.length;i++){
if(a[0] > a[i])
{
//do something
}
else if(a[0] < a[i])
{
//do something
}
}
@DawnPatrol如果他遵循你的建议,他将尝试访问一个不存在的数组元素,即[a.length]。这个解决方案没有问题。他从i = 1开始,因为他不需要比较第一个元素和它自己。 –
好点,评论删除。 – DawnPatrol
如果你想知道,如果它比所有其他值比您可以拨打最小和最大功能类似下面的
var min = Math.min.apply(null, a);
var max = Math.max.apply(null, a);
测试严格之外的其他所有值更高或更低质量以及...
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)
使用'for'循环遍历值。然后'如果'与其他项目进行比较。 – zerkms
同意@zerkms,因为您的时间复杂度不能低于N,因为您需要至少检查一次每个值以进行比较。 – Devesh
难道它不会高于某些值,低于其他值并等于其他值?或者你的意思是它是高于还是低于全部。例如,你想要的输出是更高,更低,更高,更高? –