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

174
Visualizações
Angular typescript switch case by some values

I just try to make a switch by two values.

switch ({'a': val_a,'b': val_b}){
  case ({'x','y'}):
    "some code here"
    break;
}

and this not working... any help? thanks!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Switch operator only works for primitives, it's not possible to compare objects, but you can create primitive by yourself, e.g string

const compareValue = val_a + ' ' + val_b;

switch (compareValue){
  case 'x y':
    //"some code here"
    break;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The value in a case statement must be a constant or a literal or an expression that evaluates to a constant or a literal. You can't expect a switch statement to have objects and underyling case statements have their own objects and do a comparison.

Having said that you've options in javascript that allow you to transform an object (just for comparison purpose). An approach I would following would be like this

let obj = {'a': 'x','b': 'y'};
let obj1={a:'x',b:'y'};
switch(Object.values(obj).join(','))
{
 case Object.values(obj1).join(','):
   console.log('evaluation succeeds');
 break;
}

So what I've done is took the object values and joined with a comma(effectively having a string), and in the case statement did same with another object (so that comparison could take place)

about 4 years ago · Juan Pablo Isaza Relatório

0

switch(true) {
  case obj1.a == obj2.a && obj1.b == obj2.b:
    console.log('true');
    break;
}
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