比较数组中的第一个值与其他值
Compare first value in array with others
如果数组中的第一个值比其他值高或低,建议使用什么方法进行比较
下面有一个数组
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)
相关文章:
- 而循环只设置php中输入字段中的第一个值
- 错误:$injector:modulerr模块错误(我的第一个SPA应用程序)
- IE11中的第二个调用取消了第一个Fetch API调用
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 将OnClick函数设置为<ul>,最后一个ul是擦除第一个ul-s
- RxJS油门行为;立即获取第一个值
- Angular UI网格:如何通过第一个UI网格中的按钮使第二个UI网格可见
- 选择多个实例中的第一个
- 如何在调用下一个请求之前完成第一个Ajax Get请求
- Lodash:返回对象的第一个键,该对象的值(即数组)中有一个给定的元素(即字符串)
- 看起来第一个console.log是'It’不太对
- 与杜兰达尔合作的第一个JavaScript项目.尝试从第三方 API 获取数据
- 节点发布错误对象的第一个“属性”
- 数据表删除按钮在其他页面上不起作用,除了第一个
- 悬停在第一个 DIV 上会影响所有其他第一个 DIV
- ajax只发送输入的第一个值,而不发送其他值
- jQuery只支持第一个forloop元素,而不支持Django中的其他forloop元素
- Jquery scrollTop将只滚动到第一个id,而不会滚动到其他id
- 更改第一个DropDownList的选项后,在Jquery中获取其他DropDownList的值