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

253
Visualizações
How to describe the if condition in a more efficient way in JS?

I am doing check on three different variables which are holding array of strings like below.

var a = ["ghi",dwe"]
var b = ["ghsj"]
var c = ["gdjr"]
var result; // result array

I am checking each time if variable a,b,c are not undefined and if they are holding some value I am simply pushing a string for each variable in the result array.

Below is line of code for same.

    if(a !== undefined && a.length > 0) {

    result.push("Astring");

}

if(b !== undefined && b.length > 0) {
    result.push("Bstring");

}

if(c!== undefined && c.length > 0) {
    result.push("Cstring");
}

Expected Result should be if the var a,b,c are defined and non empty then result array variable should hold value like below

["Astring","BString","CString"]

Is there a more efficient way of describing this code.Here I am just checking each and every variable using if condition. Also , I am restricted in some ways as I am using a Rhino js engine ver 1.7.12.

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

0

You could create an object with your initial data. Then iterate over it, and append the key (e.g. a, b or c) if it matches your condition.

Here's a quick demo:

// Initial Data
const a = ['ghi', 'dwe'];
const b = ['ghsj'];
const c = ['gdjr'];

// Store intial within an array
const abc = {a, b, c};

const result = [];

// Append each data item that present, is array, and not empty
Object.keys(abc).forEach((letterKey) => {
  const content = abc[letterKey];
  if(Array.isArray(content) && content.length > 0) {
    result.push(letterKey);
  }
});

// Print results
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

I'd use an object instead of an array, so that the result-elements can be specified.

var a = ["ghi","dwe"]
var b = ["ghsj"]
var c = ["gdjr"]

mapping = {
    "AString": a,
    "BString": b,
    "CString": c
}
var result = []; // result array
for (let [key, value] of Object.entries(mapping)) {
    if (value !== undefined && value.length > 0) {
        result.push(key)
    }
}

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

first you will store the 3 variables in a list then you loop over them with map

var a = ["ghi","dwe"]
var b = ["ghsj"]
var c = ["gdjr"]
var result = [];

let list = [a,b,c]

list.map(item => {
    if(item !== undefined && a.length > 0) {
        result.push(item)
    }
})
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