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

403
Visualizações
How does this nested ternary operator work using javascript?

I am trying to understand what does this nested ternary operator mean using javascript?

below is the code,

const columns = !isUser1Readable
    ? allColumns.filter(column => !columnIdsUser1.includes(column.id))
    : !isUser2Readable
    ? allColumns.filter(column => !columnIdsUser2.includes(column.id))
    : allColumns;

what i have understood?

seems like if isUser1Readable evaluates to false it filters allcolumns array such that it doesn't contain column from columnIdsUser1

if isUser1Readable is true and isUser2Readable is false then filters allcolumns array such that it doesn't contain column from columnIdsUser2

But when does allColumns is assigned to columns?

Could someone help me understand what this nested ternary operator does above. Thanks.

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

0

but when does allColumns is assigned to columns?

In the remaining case, i.e. where both isUser1Readable and isUser2Readable are true.

The chained ternary expression can be interpreted as an if ... else if ... else sequence:

let columns;
if (!isUser1Readable) {
    columns = allColumns.filter(column => !columnIdsUser1.includes(column.id));
} else if (!isUser2Readable) {
    columns = allColumns.filter(column => !columnIdsUser2.includes(column.id));
} else {
    columns = allColumns;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The conditional operator¹ is greedy. The expression is clearer with indentation:

const columns = !isUser1Readable
    ? allColumns.filter(column => !columnIdsUser1.includes(column.id))
    : !isUser2Readable
        ? allColumns.filter(column => !columnIdsUser2.includes(column.id))
        : allColumns;
  • If !isUser1Readable is true, columns is assigned the result of allColumns.filter(column => !columnIdsUser1.includes(column.id)).
  • Otherwise
    • If !isUser2Readable is true, columns is assigned the reuslt of of allColumns.filter(column => !columnIdsUser2.includes(column.id))
    • Otherwise, columns is assigned allColumns.

but when does allColumns is assigned to columns?

When both !isUser2Readable and !isUser2Readable are false.


¹ Although people frequently call it "the ternary operator," it's actually just a ternary operator (an operator accepting three operands). It's currently JavaScript's only ternary operator, but that could change. It's name is "the conditional operator."

about 4 years ago · Juan Pablo Isaza Relatório

0

As the ternary operator is define like this

condition ? exprIfTrue : exprIfFalse
  1. Condition: is the condition which is checked
  2. exprIfTrue: is the code which is process in case the Condition is evaluate to true
  3. exprIfFalse: is the code which is process in case the Condition is evaludate to false

The code which you provide can be refactor like this

let allColumns = [];
if(!isUser1Readable) {
    allColumns = allColumns.filter(column => {
        return !columnIdsUser1.includes(column.id);
    });
} else if (!isUser2Readable) {
    allColumns = allColumns.filter(column => {
        return !columnIdsUser2.includes(column.id);
    });
}else {
    columns = allColumns;
}

You can learn more about Conditional (ternary) operator

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