Bhawna Malhotra
Bhawna Malhotra

Reputation: 490

Want to check if a variable is present in an array or not

I have an array of countries.

var countriesArray = ["United States","Afghanistan","Albania","Algeria","Argentina","Australia","Austria", "Bahrain"];

And i am getting value of a country in a variable.

     <div id="location" class="editable-item">
    <dl><dt>Location</dt><dd><span class="locality"><a href="/vsearch/p?f_G=in%3A7127&amp;trk=prof-0-ovw-location" name="location" title="Find other members in Bengaluru Area, India">Bengaluru Area, India</a></span></dd><dt>Industry</dt><dd class="industry"><a href="/vsearch/p?f_I=11&amp;trk=prof-0-ovw-industry" name="industry" title="Find other members in this industry">Management Consulting</a></dd></dl></div>
    var place = document.querySelector('#location .locality a').innerHTML;
    var splitPlace = place.split(',');
    var length = splitPlace.length;
    var lastValue = splitPlace[length - 1] ;

here lastValue has value Algeria.

I want to check if the value in lastValue variable is present in countriesArray or not.For this i used

console.log($.inArray(lastValue, countriesArray));

But it is always returning -1 whether or not value is present in array or not.Suggest the mistake in this or give some other way of doing it. Also i want that the comparison of values is done irrespective of case.

Upvotes: 4

Views: 157

Answers (4)

mplungjan
mplungjan

Reputation: 177885

  • Use a lowercase array and toLowerCase() to be case insensitive.
  • Remember to trim the value too

Here is a COMPLETE example

var countriesArray = ["united states", "afghanistan", "albania", "algeria", "argentina", "australia", "austria", "bahrain"];
$(function() {
  var place = $('#location .locality a').html();
  var splitPlace = place.split(',');
  var length = splitPlace.length;
  var lastValue = $.trim(splitPlace[length - 1]);
  var found = $.inArray(lastValue.toLowerCase(), countriesArray)!=-1;
  alert(lastValue+" was "+(found?"found":"not found"))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="location"><span class="locality"><a href=#">Algeria,Argentina,Australia,Austria,Bahrain</a></span></div>


Native alternative to $.inArray - indexOf var last = countriesArray[countriesArray.length-1]; alert(countriesArray.indexOf(last.toLowerCase().trim())!=-1)

Upvotes: 2

Ram
Ram

Reputation: 144689

In your code lastValue is " India". It starts with a space character. You should trim the string, using jQuery $.trim or String.prototype.trim function. Also the array doesn't have India element so $.inArray should still return -1.

$.inArray($.trim(lastValue), countriesArray);

In case that the search should be case insensitive, you can use the String.prototype.toLowerCase or String.prototype.toUpperCase method:

$.inArray(lastValue.trim().toLowerCase(), countriesArray.map(function(el) {
    return el.toLowerCase();
}));

Upvotes: 2

adnan kamili
adnan kamili

Reputation: 9455

Following should do it:

if(countriesArray.indexOf(lastValue) >= 0 )
{
   alert("present");
}
else 
{
   alert("not present");
}

If you want it to be case insensitive than better iterate

for(var i=0; i<lastValue.length; i++)
{
   if(lastValue.toLowerCase().trim() == countriesArray[i].toLowerCase().trim())
   {
       alert("present");
       break;
   }
}

Upvotes: 0

Brijesh Bhatt
Brijesh Bhatt

Reputation: 3830

Try this, use .trim() to trim the value as it may contains leading and trailing spaces:

if(countriesArray.toString().indexOf(lastValue.trim())>=0) {
  alert(lastValue)
}

FIDDLE DEMO

Upvotes: 0

Related Questions