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

7

What am I doing wrong in this if ?

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

If I only do this, it works. But I'll have to repeat it to others.

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

I'm using this code inside my 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 the help !!

    
asked by anonymous 25.12.2015 / 20:57

3 answers

6

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

  

The in operator returns true if the specified property is in the specified object.

Source: in operator (MDN)

To verify that an item is contained in an array , which appears to have been your intention, you will need to use the #

if (['A', 'E', 'I', 'O', 'U'].indexOf(angular.uppercase(nome[id])) != -1) {
    dados[id] = 'é vogal';
} else {
    dados[id] = 'não é vogal';
}
    
25.12.2015 / 21:57
4

Try to do this:

$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's a bit "ugly" this way, but it's functional. Test your code.

The in of javascript operator is used for checking keys in objects and in your case you have an array .

A check on arrays is actually done the way @Zignd spoke. But you can also transform 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 this, just do it like this:

if (angular.uppercase(nome[id]) in toObject(['A', 'E', 'I', 'O', 'U'])) {
  dados[id] = 'é vogal';
} else {
  dados[id] = 'não é vogal';
}
    
25.12.2015 / 21:58
4

Much has already been said in 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 this variable globally and you may be overriding values in other variables that may have the same name. Always add var to for (var i = ... .

In your code you are creating an object with a cash index. 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) to map the initial string. I also leave a suggestion with regex, which dispenses .indexOf and uppercase .

In this case the code you want could look 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 this:

(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.

    
26.12.2015 / 01:44