How to check if an item is contained in an array?

Asked

Viewed 6,440 times

7

What am I doing wrong in this if?

if (angular.uppercase(nome[id]) in ['A', 'E','I', 'O', 'U']) {
    ....
}

If I only do so it works. But I will have to repeat to others.

if (angular.uppercase(nome[id]) == 'A'){
    .....
}

I’m using this code inside mine controller.

$scope.verificaVogal = function(nome) {
  var dados = {};
  for (id = 0; id < nome.length; id++) {
    if (angular.uppercase(nome[id]) in ['A', 'E', 'I', 'O', 'U']) {
      dados[id] = 'é vogal';
    } else {
      dados[id] = 'não é vogal';
    }
  }
  return dados;
}

Thanks for your help!!

3 answers

6


You are using the operator in incorrectly, its function is to return true if the specified property is in the specified object. In your case you are working with a array and not an object.

The in Operator Returns true if the specified Property is in the specified Object.

Source: in Operator (MDN)

To check if an item is contained in a array, which appears to have been its intention, it will be necessary to use the indexOf as follows:

if (['A', 'E', 'I', 'O', 'U'].indexOf(angular.uppercase(nome[id])) != -1) {
    dados[id] = 'é vogal';
} else {
    dados[id] = 'não é vogal';
}
  • Thanks for the help Zignd!!!

4

Try to do so:

$scope.verificaVogal = function(nome) {
  var dados = {};
  for (id = 0; id < nome.length; id++) {
    if (angular.uppercase(nome[id]) in {'A':'', 'E':'', 'I':'', 'O':'', 'U':''}) {
      dados[id] = 'é vogal';
    } else {
      dados[id] = 'não é vogal';
    }
  }
  return dados;
}

It gets a little "ugly" that way, but it’s functional. Test in your code.

The operator in javascript is used for key checking in objects and in your case you own a array.

A check on arrays is actually done the way @Zignd said. But you can also turn the arrays (arrays) into objects with the following function:

function toObject(array){
  var object = {};
  for(var i = 0; i < array.length; i++){
    object[array[i]]='';
  }
  return object;
}

With that, just do so:

if (angular.uppercase(nome[id]) in toObject(['A', 'E', 'I', 'O', 'U'])) {
  dados[id] = 'é vogal';
} else {
  dados[id] = 'não é vogal';
}
  • Thanks for the help Samir Braga!!!

4

It has already been said most of the other answers. I leave another answer with some things not yet said.

Note that when you use for (id = 0; id you are declaring that variable globally and may be overwriting values in other variables that may have the same name. Always join var in for (var i = ....

In your code you are creating an object with index cash. In this case you will have something like

{
    0: 'string',
    1: 'outra string',
    etc...

and so it seems to me more semantic (ie more correct) map the initial string. I also leave a suggestion with regex, which dispenses with the .indexOf and uppercase.

In which case the code you want could be like this:

$scope.verificaVogal = function(nome) {
    var vogais = ['A', 'E', 'I', 'O', 'U'];
    return nome.split('').map(function(letra) {
        return vogais.indexOf(angular.uppercase(letra) != -1);
    });
}

or so:

$scope.verificaVogal = function(nome) {
    return nome.match(/[aeiou]/i);
}

Note: if you use the first variant, I prefer it this way:

(function(){
    var vogais = ['a', 'e', 'i', 'o', 'u'];
    $scope.verificaVogal = function(nome) {
        return nome.split('').map(function(letra) {
                return vogais.indexOf(letra.toLowerCase()) != -1);
        });
    }
})();

to decrease whenever possible non-native code and not to declare constants within the function.

  • Thanks for the help Sergio!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.