0

I have a string array like this

function checkAlreadyShortlisted()
{
    var shortListed = new Array();                    
    shortListed = '["Mr. Andrew Severinsen", "Mr. Tom Herron", "Ms. Samantha Smithson-Biggs", "Mr. Efrem Bonfiglioli", "Mr. Giles Forster"]';
    var candidateName = '';

    $('.searchResultCandidateDiv').each(function(i, obj) {
                    
        candidateName = $(this).find('.viewResumeAnchor').text();  // Get the text from the particular div
        console.log(candidateName);

        if(candidateName == shortListed[4])   // Copied the value from Set
            {
            console.log('Got from Set');
        }              
        else if(shortListed[4] == "Mr. Giles Forster") // Copied the value from anchor
        {
            console.log('Got from Anchron text');
        }
    });
}

In the div looped I have to check if the name in the shortlisted array is present or not.

These are values I copied from browser console log

Mr. Efrem Bonfiglioli

Mr. Giles Forster

Checking condition with above text is working fine, but if I try to check the values with array string it is not working properly. But text are similar any ideas?

0

1 Answer 1

1

Variable shortListed not holding array values, but strings. Remove quote ' around it :

shortListed = ["Mr. Andrew Severinsen", "Mr. Tom Herron", "Ms. Samantha Smithson-Biggs", "Mr. Efrem Bonfiglioli", "Mr. Giles Forster"];

Then you're able to accessing it by shortListed[0], shortListed[1], shortListed[n],..

More easy way to check array values contains something, i would like to use $.inArray() built in function in jQuery. As this function will return -1 if value did't not existed, otherwise will returned index if found that. See example below how to use that :

if ( $.inArray('Mr. Tom Herron',shortListed) !== -1 ) {    
  alert('Found it');
}

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.