Home > Back-end >  ID match with different fields using if condtion in JS?
ID match with different fields using if condtion in JS?

Time:03-07

I Have two objects booking and History. I have to check booking userId matches with History userId or History CustomerID

If booking userId matches with any of these two fields(History userId or History CustomerID) we should return "ID matched" in the console.

If booking userId does not match with any of these two fields(History userId or History CustomerID). we should not return anything.

Below is my code .its working as expected but is this a better approach? or i can do this in some other way

Please suggest

var booking = {"userId":"1233","CustomerID":null,"username":"av"};
var History = {"userId":"123","CustomerID":null,"username":"av"};

var a = booking.userId != History.userId;
var b = booking.userId == History.CustomerID;
var c = booking.userId == History.userId;
var d = booking.userId != History.CustomerID;
console.log(a)
console.log(b)
console.log(c)
console.log(d)

if( a && !b || c && !d)
{
console.log("ID not mathced with booking ")
}

CodePudding user response:

A way to check if a variable matches multiple values is as follows:

const match = [History.userId, History.CustomerID].includes(booking.userId);

It creates an array of the possible values and checks if any of them match the value to be matched.

CodePudding user response:

If you look at your boolean definitions, you can notice that c and d are redundant, that's because c is the same as !a, and d is the same as !b. You can get rid of them and just use a and b. I'd also suggest using inequality for both, just for consistancy. In this case, the code becomes:

const booking = { "userId": "1233", "CustomerID": null, "username": "av" };
const History = { "userId": "123", "CustomerID": null, "username": "av" };
const a = booking.userId !== History.userId;
const b = booking.userId !== History.CustomerID;

if (a && b) {
    console.log('ID not matched with booking');
}

You can also omit the variables definitions and directly do:

if (booking.userId !== History.userId && booking.userId !== History.CustomerID)

And if the list of things to compare to gets longer, you can use the following trick to reduce the size of the if statement:

if (![History.userId, History.CustomerID].includes(booking.userId))

CodePudding user response:

Like this...

var booking = {"userId":"1233","CustomerID":null,"username":"av"};
var History = {"userId":"123","CustomerID":null,"username":"av"};

var match = booking.userId === History.userId || booking.userId === History.CustomerID;
if  (match)
  console.log("ID mathced")
else
  console.log("ID not mathced with booking ")
  

CodePudding user response:

Here you go. The answer is in your problem statement booking userId matches with History userId or History CustomerID

var booking = {
  "userId": "1233",
  "CustomerID": null,
  "username": "av"
};
var History = {
  "userId": "123",
  "CustomerID": null,
  "username": "av"
};

if (booking.userId === History.userId || booking.userId === History.CustomerID) {
  console.log('ID matched.');
} else {
  console.log('ID not matched with booking.');
}

CodePudding user response:

You can use like this the best way and the easiest way I think.

var booking = {"userId":"1233","CustomerID":null,"username":"av"};
var History = {"userId":"123","CustomerID":null,"username":"av"};

 booking.userId === History.userId ? console.log("ID Matched") : console.log("ID not matched with booking ID");

  • Related