Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda