Nicks Nicks - 4 months ago 11
Javascript Question

Search string inside array javascript

I am looking for a simple function to check if a specific string is inside my array.

function checkRegion(departement){
var region = '';

if(alsace.indexOf(departement) != -1)
{
region = "alsace";
}
if(aquitaine.indexOf(departement) != -1){
region = "aquitaine";
}
if(auvergne.indexOf(departement) != -1){
region = "auvergne";
}
if(basseNormandie.indexOf(departement) != -1){
region = "basse-normandie";
}
if(iledefrance.indexOf(departement) != -1){
region = "ile-de-france";
}else{
region = 'undennnnnfined';
}

return region;
};


Any solution ?

Thanks

Answer

Your problem lies in the use of consecutive if statements without chaining them together to make a complete check.

Doing it your way, the code actually completely disregards all the if statements, but the last one.

So, if iledefrance.indexOf(departement) != -1 gives false, it'will always execute the code inside else, meaning it'll set region = 'undennnnnfined'.


Note:

In the code, I replaced != -1 with ~ as it makes for a somewhat more succinct code. In essence, it will convert -1 to 0, namely false.

Be sure to check out MDN's documentation for more if you are not familiar.


1st Option :

Try chaining your if statements together in an if/else if/else format as follows:

function checkRegion(departement){
    var region = '';

    if(~alsace.indexOf(departement)) {
        region = "alsace";
    }
    else if(~aquitaine.indexOf(departement)) {
        region = "aquitaine";
    }
    else if(~auvergne.indexOf(departement)) {
        region = "auvergne";
    }
    else if(~basseNormandie.indexOf(departement)) {
        region = "basse-normandie";
    }
    else if(~iledefrance.indexOf(departement)) {
        region = "ile-de-france";
    }
    else{
        region = 'undennnnnfined';
    }
    return region;
};

2nd Option :

Create two arrays:

  • One should contain your region arrays (alsace, aquitaine etc) &
  • One more containing the names of your arrays as strings, so that you can return the appropriate string based on the array that was evaluated as true.

Code:

function checkRegion(departement) {
    var
       regions = [alsace, aquitaine, auvergne, basseNormandie, iledefrance],
       regionsNames = ["alsace", "aquitaine", "auvergne", "basseNormandie", "iledefrance"];

    for (var i = 0; i < regions.length; i++) {
        if (~regions[i].indexOf(departement)) {
            return regionsNames[i];
        }
    }
    return "undennnnnfined"; // If the 'for' loop doesn't return anything, it's false.
};

Demos:

  • Working demo with the 1st solution → here.
  • Working demo with the 2nd solution → here.

Snippets:

  • Snippet with the 1st solution:

function checkRegion(departement){
    var region = '';

    if(~alsace.indexOf(departement)) {
        region = "alsace";
    }
    else if(~aquitaine.indexOf(departement)) {
        region = "aquitaine";
    }
    else if(~auvergne.indexOf(departement)) {
        region = "auvergne";
    }
    else if(~basseNormandie.indexOf(departement)) {
       region = "basse-normandie";
    }
    else if(~iledefrance.indexOf(departement)) {
        region = "ile-de-france";
		}
    else{
        region = 'undennnnnfined';
    }
    return region;
};

var
   alsace = ["Strasbourg", "Colmar"],
   aquitaine = ["Gironde", "Landes", "Dordogne", "Pyrenees-Atlantiques", "Lot-et-Garonne"],
   auvergne = [],
   basseNormandie = [],
   iledefrance = [];
   
alert(checkRegion("Strasbourg"));

  • Snippet with the 2nd solution:

function checkRegion(departement) {
    var
       regions = [alsace, aquitaine, auvergne, basseNormandie, iledefrance],
       regionsNames = ["alsace", "aquitaine", "auvergne", "basseNormandie", "iledefrance"];

    for (var i = 0; i < regions.length; i++) {
        if (~regions[i].indexOf(departement)) {
            return regionsNames[i];
        }
    }
    return "undennnnnfined"; // If the 'for' loop doesn't return anything, it's false.
};

var
   alsace = ["Strasbourg", "Colmar"],
   aquitaine = ["Gironde", "Landes", "Dordogne", "Pyrenees-Atlantiques", "Lot-et-Garonne"],
   auvergne = [],
   basseNormandie = [],
   iledefrance = [];
   
alert(checkRegion("Strasbourg"));