Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7746307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:14:47+00:00 2026-06-01T10:14:47+00:00

I’m trying to search for a string in an array. If that string matches,

  • 0

I’m trying to search for a string in an array. If that string matches, I want to know which index number in that array has the matching string. I believe I should be using inArray(); but it’s always returning -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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:14:48+00:00Added an answer on June 1, 2026 at 10:14 am

    This may not help you solve your original question about inArray() returning -1, but I wanted to shed some light on the issues the other users had pointed out.

    Alternatives to eval()

    • If your $op* variables exist in global scope you could use the window object and access them from there:

       var myArray = window['$op' + myselect];
      
    • Better yet, you can remove your variables from 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 function-level scoped; not blocked-level scoped. In block-level scoped languages variables declared inside a conditional block are local to that block. In JavaScript this is not the case, and you can run into some strange behavior, such as variable hoisting. I’m not going in to detail on JavaScript scoping, as this post is already getting too long, but this awesome article does a great job explaining it in detail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.