Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
ID match with different fields using if condtion in JS?

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 ")
}

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

0

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 ")
  

about 4 years ago · Juan Pablo Isaza Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda