javascript - Want to check if a variable is present in an array or not -
i have array of countries.
var countriesarray = ["united states","afghanistan","albania","algeria","argentina","australia","austria", "bahrain"]; and getting value of country in variable.
<div id="location" class="editable-item"> <dl><dt>location</dt><dd><span class="locality"><a href="/vsearch/p?f_g=in%3a7127&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&trk=prof-0-ovw-industry" name="industry" title="find other members in 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 check if value in lastvalue variable present in countriesarray or not.for used
console.log($.inarray(lastvalue, countriesarray)); but returning -1 whether or not value present in array or not.suggest mistake in or give other way of doing it. want comparison of values done irrespective of case.
in code lastvalue " india". starts space character. should trim string, using jquery $.trim or string.prototype.trim function. array doesn't have india element $.inarray should still return -1.
$.inarray($.trim(lastvalue), countriesarray); in case search should case insensitive, can use string.prototype.tolowercase or string.prototype.touppercase method:
$.inarray(lastvalue.trim().tolowercase(), countriesarray.map(function(el) { return el.tolowercase(); }));
Comments
Post a Comment