Finding string variable with jquery inarray () returns -1
I am trying to find a string in an array. If this line matches, I want to know what index number in this array the corresponding line has. I believe I should use inArray (); but it always returns -1
var $eventwrap = $j('.tw-events'),
$daywrap = $j('.tw-day'),
$dayfilter = $j('#tw-filter-days li a');
$daywraphide = $j('tw-day.hide'),
$catwrap = $j('.tw-event-filter'),
$viewctrls = $j('.tw-view a'),
// RELEVANT CODE STARTS HERE
$clonedays = $j('.select-days').html(),
$clonebarrio = $j('.select-barrio').html(),
$clonecats = $j('.select-cats').html(),
$opday = $clonedays.split("</option>"),
$opbarrio = $clonebarrio.split("</option>"),
$opcategory = $clonecats.split("</option>");
// RELEVANT CODE ENDS HERE
filters = {};
// CHECK IF A GIVEN DAY HAS EVENTS
function filterToggle(element,x,y) {
$j(element).each(function(){
var $me = $j(this),
isli = $me.is('li');
if(isli) {
var myvalue = $me.find('a').attr('data-filter');
} else {
// RELEVANT CODE STARTS HERE
var myselect = $me.parent().attr('data-filter-group'),
myvalue = $me.attr('data-filter'),
myfilter = String(myvalue);
// RELEVANT CODE ENDS HERE
}
if(!x) {x = ''}
if(!y) {y = ''}
var eventcount = $j('.tw-event'+ myvalue + x + y).length;
if(eventcount == 0) {
if(isli) {
$me.addClass('empty tdn');
} else {
$me.remove();
}
} else {
if(isli) {
$me.removeClass('empty tdn');
} else {
// RELEVANT CODE STARTS HERE
var myarray = eval("(" + '$op' + myselect + ")");
alert($j.inArray(myfilter,myarray));
// RELEVANT CODE ENDS HERE
}
}
});
}
What am I doing wrong?
This may not help you solve your original question about inArray () returning -1, but I wanted to shed some light on the issues other users have pointed out.
Alternatives eval()
-
If the $ op * variables exist in the global scope, you can use the window object and access from them:
var myArray = window['$op' + myselect];
-
Better yet, you can remove your variables from the global scope by placing them in a module. Maybe try something like this:
var myNamespace = (function() { var $eventwrap = $j('.tw-events'), $daywrap = $j('.tw-day'), $dayfilter = $j('#tw-filter-days li a'), ... $opday = $clonedays.split("</option>"), $opbarrio = $clonebarrio.split("</option>"), $opcategory = $clonecats.split("</option>"); return { opday: $opday, opbarrio: $opbarrio, ... //whatever you need to access externally goes in the return object } })(); //Now you can access these variables like so function filterToggle(element,x,y) { ... var myarray = myNamespace['$op' + myselect]; ... }
Variable declarations
Javascript is a functional level area; not locked level. In block cloud languages, variables declared inside a conditional block are local to that block. This is not the case in JavaScript, and you may encounter some strange behavior, such as variable hoisting. I won't go into JavaScript in detail as this post is already too long, but this awesome article does a great job of it in detail.
I'm not sure I fully understand what you are trying to do by looking at your code, but I think the whole approach looks weird and WET , I would probably rethink the whole thing. You have serious syntax errors; a lot of mischief ;
in your ads var
. It is also not recommended to declare variables inside conditional blocks. From jslint -> eval === evil
.
var arrayString = new Array();
var string = "this is the data to insert in array";
arrayString = string.split(" ");
//checking for data in above array
$.each(arrayString, function(i, val) {
alert(arrayString[i]);
if (arrayString[i] == "data") {
alert(i);//i gives the index of the found element
}
});