admin管理员组文章数量:1403452
I have a variable with number and array. For example:
var arr = [10, 13, 17];
var number = 8;
I want to check if number is less then every value in arr and if true do something like this:
var arr = [10, 13, 17];
var number = 8;
if (number < arr[]) {
// do something
} else {
// do something
}
Could you please help me to solve this issue?
I have a variable with number and array. For example:
var arr = [10, 13, 17];
var number = 8;
I want to check if number is less then every value in arr and if true do something like this:
var arr = [10, 13, 17];
var number = 8;
if (number < arr[]) {
// do something
} else {
// do something
}
Could you please help me to solve this issue?
Share Improve this question edited Nov 10, 2019 at 16:17 Sergi Khizanishvili asked Nov 10, 2019 at 16:06 Sergi KhizanishviliSergi Khizanishvili 5871 gold badge8 silver badges17 bronze badges 7-
2
arr.some(e => number < e)
– ASDFGerte Commented Nov 10, 2019 at 16:06 -
@ASDFGerte - "I want to check if number is less then any all values in arr..." I think you meant
every
, notsome
. – T.J. Crowder Commented Nov 10, 2019 at 16:11 - @T.J.Crowder I read the title, and went by that. – ASDFGerte Commented Nov 10, 2019 at 16:11
- 2 The question is ungrammatical and ambiguous: "any all values" should be either "any values" or "all values", so the question needs to be edited to clarify this. – kaya3 Commented Nov 10, 2019 at 16:12
- @ASDFGerte - Quite. :-) – T.J. Crowder Commented Nov 10, 2019 at 16:12
7 Answers
Reset to default 7JavaScript doesn't provide a way to do that for you, you have to do it yourself.
I want to check if number is less then any all values in arr...
It would be either "any value in arr
" or "all values in arr
". "any all values in arr
" doesn't make sense.
If you mean "number
is less than all values in arr
", probably the easiest way is the every
function on arrays:
if (arr.every(e => number < e)) {
If you mean "number
is less than any value in arr
", you want some
instead:
if (arr.some(e => number < e)) {
One good thing about that is they short-circuit: As soon as every
/some
knows the result, it stops looping. For every
, that means the first time the callback returns a falsy value; for some
, that means the first time the callback returns a truthy value.
var arr = [10, 13, 17];
var number = 8,
number1 = 12;
if (number < Math.min(...arr)) {
console.log("do something");
} else {
console.log("do something else");
}
if (number1 < Math.min(...arr)) {
console.log("do something");
} else {
console.log("do something else");
}
How about the use of min() function?
Math.min(...arr);
should return the minimum value of the array. Its an ES6 function.
Simple approach is to check for the minimum of an array and then pare your variable against that. If your variable is the lowest number then it will be equal to the minimum of an array.
var arr = [10, 13, 17];
var number = 8;
if(number > Math.min.apply(null, arr)) {
console.log("at least one value is lower than your number");
} else {
console.log("no value is lower than your number");
}
Here are two examples on how to achieve that:
var arr = [10, 13, 17];
var number = 8;
// Old school version
const isSmallest = (n, list) => {
let hasSmaller = false
for (let i = 0; hasSmaller === false && i < list.length; i++) {
hasSmaller = n > list[i];
}
return !hasSmaller;
}
// Array.prototype.every version
const isSmallest2 = (n, list) => {
return list.every(item => n < item)
}
console.log(isSmallest(number, arr));
console.log(isSmallest2(number, arr));
In your case you can use for..in, for of, map and filter. Lets see 2 examples.
E.g for of
var arr = [10, 13, 17];
var number = 8;
for(const element of arr) {
if( number < element){
// do something
console.log(`${number} > ${element}`)
}
else {
//do something
console.log(`${number} <= ${element}`)
}
}
E.g E.g map
var arr = [10, 13, 17];
var number = 8;
arr.map((element) => number < element ? console.log(`${number} > ${element}`) : console.log(`${number} <= ${element}`))
Here is an example for performance;
function isMin(num){
for (let i = 0; i < arr.length; i++) {
if (arr[i]<num){
return false
}
}
return true
}
本文标签: javascriptCheck if variable is less then every value in arrayStack Overflow
版权声明:本文标题:javascript - Check if variable is less then every value in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744401974a2604509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论