0

I am trying to write some logic that will check for the existence of a specific record in two tables. If the row exists in either table, I want to return TRUE.

For table A, when the record is added to the table, the id field looks like "123". For table B, the same record would have the id of "a123". However, the value i have to search for the record is "row_123".

This is what my code looks like right now :

var checkForDuplicates = function(row_id) {

   return !!($('#existing_members').find('#' + row_id.replace('row_','')).length || $('#selected_users').find('#' + row_id.replace('row_','a').length) );

};

I want the function to return true if the record exists in either table.
However, this statement returns true in cases when it should be false.

What I've tried so Far I've been playing around in the console to make sure that my logic is correct:

!!(1 || 0)  //returns true
!!(0 || 0)  //returns false
!!(0 || 1)  //returns true

I'm also currently reviewing the replace statements to make sure the find() is being supplied the right strings.

But a second pair of eyes to confirm that my logic is correct would be appreciated.

Thanks

EDIT 1

The solution, using Max's suggestion would be:

  var checkForDuplicates = function(row_id) {
             var parts = row_id.split('_');
             var tableB = '#a'+ parts[1];
             var tableA = '#' + parts[1];
             return !!($('#existing_members').find(tableA).length || $('#selected_users').find(tableB).length);
 }

However, as Ankit points out, I just had a typo in my original code. So this would be my final answer / solution:

var checkForDuplicates(row_id) {
       return !!( $('#existing_members').find('#' + row_id.replace('row_', '')).length || $('#selected_users').find('#' + row_id.replace('row_','a')).length);
}
3
  • Try console.loging the values of $('#tableA').find('#' + row_id.replace('row_','')).length and $('#tableB').find('#' + row_id.replace('row_','a').length). On which values do they fail? Commented Jun 17, 2015 at 14:17
  • 3
    In the first paragraph of your question you write: "If the row exists in either table, I want to return false" and later: "I want the function to return true if the record exists in either table". Commented Jun 17, 2015 at 14:21
  • Please rephrase your question and provide us with an example... Commented Jun 17, 2015 at 14:25

4 Answers 4

1

Your code has a typo at the end of return statement

...'a').length));  //it returns object which always evaluates to true

it should be

...'a')).length);   

DEMO

	var checkforduplicates = function(row_id){
     //row_id looks like "row_123"
	 return !!($('#tableA').find('#' + row_id.replace('row_','')).length || $('#tableB').find('#' + row_id.replace('row_','a')).length );
	
}
alert(checkforduplicates("row_123"));
<table id=tableA><tr><td id="123">123 ID</td></tr></table>
	<table id=tableA><tr><td id="a13">a13 ID</td></tr></table>

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

Comments

1

Corrected few issues to make the code more efficient:

var checkforduplicates = function(row_id) {
   var id = row_id.split('_')[1];  // [ 'row', '123']
   return $('#'+id).length || $('#a'+id).length;
}
  • No need for !! as operator || produces boolean result (true or false)
  • Used $('#'+id) as more efficient jQuery selector
  • Removed unnecessary find(..) call
  • Eliminated unnecessary parenthesis (which had an issue)

4 Comments

ah sorry for the confusion. if the element exists in any table, i want it to return true... aka... there is a duplicate
Then the current code should work - please try and let me know.
yes, your suggestion of breaking up the row_id into parts was good. It makes things clearer. I used most of your code, but just to prove that it can be done... I wrapped the return statement with !!( your code) because I wanted to see if it returns "false" instead of 0. It works. Please see edit 1 in my post for the final code.
max, +1 for your solution. very clear. but ... i have to select Ankit's answer because it is what i was after.
0

I want the function to return true if the record exists in either table.

var checkForDuplicates = function(row_id) {
    row_id = row_id.substring(4); // 'row_123' -> '123'

    var table_A_row_id = row_id;
    var table_A_row_exists = $('#tableA').find('#' + table_A_row_id).length > 0;

    var table_B_row_id = 'a' + row_id;
    var table_B_row_exists = $('#tableB').find('#' + table_B_row_id).length > 0;

    return table_A_row_exists || table_B_row_exists;
};

1 Comment

aha, "in either table" means "in one of the two", not in both. OK, then you should user logical OR, ie ||
-1

of course it is returning the opposite of the things you want, cause you are using !!.

! is used to negotiate the return value of the specific function/variable e.g.:

if(!var_x == false)

this example only works if var_x is true. So please be aware to avoid !! ;)

Please use a single ! instead!

4 Comments

Snickbrack, i thought !! just converted 0 to 'false' and 1 to 'true'?
nah nah, 0 is equals false and 1 is equals to true
yes, i know that in jscript 0 == false and 1 == true. but if you wanted to explicitly return "false" instead of 0, i do believe you can use !!. I just tested in the console to prove that it works.
yeah you can use !! i don#t say you can't, but please re-read my post. your example alert(!!true); for example do not work.

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.