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 ")
}
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.
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 ")
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.');
}