I have a variable that is set dynamically on the page. I then have a list of colors in an array. I need to see if the color variable contains one of the items within the array.
Here's the code:
var colorlist = ['Silver', 'Gray', 'Black', 'Red', 'Purple', 'White'];
var col1 = "";
var color1 = 'Titanium Silver';
for (var c = 0; c < colorlist.length; c++)
{
if(color1.indexOf(colorlist[c]))
{
col1 = colorlist[c];
}
else
{
}
}
What I would expect this to return is "Silver", but it's consistently returning the last item in the array. What am I doing wrong?
indexOfyou may want to usesearchArray.indexOf, butString.indexOfis fine.