How to check if at least one item in the array has a value greater than or equal to 2

5

I have the following array:

array[0,2,0,0];

I need to create a function that returns true if at least one item in the array has a value equal to or greater than 2

    
asked by anonymous 01.11.2016 / 17:52

7 answers

10

You can make a filter and check the resulting array size as below:

var teste = [0,2,0,0];

function verificar(array) {
  return array.filter(function(item) {
    return item >= 2;
  }).length > 0;
}

console.log(verificar(teste));
    
01.11.2016 / 17:59
8

Try user the array function some

Check if there is any element whose value is equal to 2

var x = [0, 0, 2, 0];

function isTwo(value) {
  return value === 2;
}

var y = x.some(isTwo);

console.log(y);

Check if there is any element greater than or equal to 2

var x = [0, 0, 1, 0];

function maiorQueUm(value) {
  return value >= 2;
}

var y = x.some(maiorQueUm);

console.log(y);

ES6's arrow functions would be even simpler:

var x = [0, 0, 1, 0];
var y = x.some(it => it >= 2);

var w = [0, 0, 2, 0];
var z = w.some(it => it >= 2);

console.log(y, z);
    
01.11.2016 / 17:57
6

According to your question, it is enough to know if there is a number greater than or equal to two, in this case, a simple loop is sufficient ...

function checkArray(myArray) {
    for (i = 0; i < myArray.length; i++) { 
        if(myArray[i] >= 2){
          return true;
        }
    }
    return false;
}

var myArray = array[0,2,0,0];
alert(checkArray(myArray));
    
01.11.2016 / 17:57
6

Here are several different ways:

var arraryValida = [0, 2, 0, 0];
var arraryInvalida = [0, 1, 0, 0];
var valor = 2;

function testeFor(arr, match) {
    for (var x = 0, l = arr.length; x < l; x++) {
        if (arr[x] >= match) return true;
    }
    return false;
}

function testeMax(arr, match) {
    return Math.max.apply(Math.max, arr) >= match;
}

function testeSome(arr, match) {
    return arr.some(nr => nr >= match);
}

[testeFor, testeMax, testeSome].forEach(function(fn, i) {
    console.log(fn.call(null, arraryValida, valor));
    console.log(fn.call(null, arraryInvalida, valor));
	console.log(i, '----');
});

jsFiddle: link

    
01.11.2016 / 18:42
5

Here's a simple way to find indexes :

function checkIndexTwoOrLarger(arr) {
    var achou = false;
    arr.forEach(function(v, i) {
        if (i >= 2)
           achou = true;
    });
    return achou;
}
checkIndexTwoOrLarger([0,0,0,2]);

For values , it would look something like this:

function checkValueTwoOrLarger(arr) {
    var achou = false;
    arr.forEach(function(v, i) {
        if (v >= 2)
           achou = true;
    });
    return achou;
}
checkValueTwoOrLarger([0,0,0,2]);
    
01.11.2016 / 18:47
3

Array # some is the best option: / p>

function funcaoMaiorIgualQueDois(element, index, array) {
  return element >= 2;
}

var  res = [0,2,0,0].some(funcaoMaiorIgualQueDois);
console.log(res);
    
01.11.2016 / 18:29
3

In the series of questions that the AP is asking ( [1] , [2] ), very similar, I will insist on simplicity, performance and even readability, although the latter is a subjective way of evaluating . I would do with a simple for and I do not want to think that you are optimizing when using a array size cache ( I have already shown that this does not work on modern JS engines ):

function temValor(array) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] >= 2) {
            return true;
        }
    }
    return false;
}
var array = [0, 5, 1, 2];
console.log(temValor(array));

If you want to generalize the limit:

function temValor(array, limite) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] >= limite) {
            return true;
        }
    }
    return false;
}
var array = [0, 5, 1, 2];
console.log(temValor(array, 2));
    
01.11.2016 / 19:13