Stack Exchange
How to compare not equal value in Array and database fields value in Javascript?
I have this variables getting specific value in loop from database:
var _ID;

for(var x = 0;x<id.length;x++){
    _ID = id[x];
}

Now, I have set array:
var _comID =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];

Then I'm comparing this value to _ID in first loop from database(full code below):
I have this variables getting specific value in loop from database:
code
var _ID;
var _comID =[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];


for(var x = 0;x<id.length;x++){
    _ID = id[x];//just to show
     for (z=0;z<_comID.length;z++){
        
        if(_comID[z] == id[x] ){
           console.log(_comID[z]); * //this print the equal value, how ever if not equal can't*
        }
        else{
            console.log(_comID[z]); *// this print all the value from 1 - 26. Please note that the value from  id[x] or _ID are random from 1 - 26 and sometimes its give only 18 to 23 numbers*
          }
    }
}


Hi Cerberus Welcome to Stackoverflow, could you please specify what is your expected output?

Hi - since equal condition has output correctly. It gives the  equal value:
ex. from database for "==" it gives the output:
_ID or  id[x] = 1,4,6,10,11,12,14,14,15,18,21,24,26  -> equal value that has _comID. Now my expected value is for " NOT EQUAL/ !== " is get/print the value from _comID which is not equal. You can see the expected value also based on the logic of if else statement

That's fine, then what is the issue which you are facing? Do you want to refactor/optimize the code or what needs to be done that's is not getting cleared.

Hi Cerberus, What are you trying to do ? Compare array ? find only matched value ?

Hi Himanshu - Sorry if its not clear. I'm asking a suggestion how to do this correctly based on the logic.


Get the matched value between _ID(from database) and _comID(set array). This is already correct since I got the correct output for all matched value in "IF" statement


Get the value which is not Matched. Its in ELSE statement. Because on that code above its give the output of all array value. Does any suggestion for this one?



because i have tried evrything to get the NOT MATCHED value even doing a filter based on the _ID(from database) value. This number is what im trying to achieved which must GET THE NOT EQUAL or NOT MATCHED VALUE

Hi Nur - I need the NOT EQUAL or NOT MATCHED VALUE  from this array which comapred from _ID(from DATABASE). I tried multiple logic but stiil cant get the NOT MATCHED value. I only get a MATCH VALUE correctly.

To find not equal values use this:  id.filter(_ID => !_comID.includes(_ID)); Is this answer your question?
