return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - Check if variable is less then every value in array - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if variable is less then every value in array - Stack Overflow

programmeradmin4浏览0评论

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, not some. – 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
 |  Show 2 more ments

7 Answers 7

Reset to default 7

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 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
}
发布评论

评论列表(0)

  1. 暂无评论