简体   繁体   中英

Check if variable is less then every value in array

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?

JavaScript 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 compare 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.

Eg 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}`) } }

Eg Eg 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM