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

235
Visualizações
Lodash replacement for a JS check operation to set value

I have been using some code to check the value for a input field which is present at index:1 of an array. It is as below

const inputValue=journeyData && journeyData.birthdate && journeyData.birthdate[0]
          ? journeyData.birthdate[0]
          : null, 

Is there a lodash replacement for the same thing which makes it shorted to write ?

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

0

Use native optional chaining. No need for lodash.

const journeyData = { birthdate: [new Date()] };

const inputValue = journeyData?.birthdate?.[0] ?? null;
const inputValue2 = journeyData?.birthdate?.[2] ?? null;

console.log(inputValue);
console.log(inputValue2);

Additional documentation

  • Nullish coalescing operator
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use _.get() which allows specifying a path and also a fallback. If the path cannot be reached on the object, you get the default value:

function test(journeyData) {
  const inputValue = _.get(journeyData, "birthdate[0]", null);
  return inputValue;
}

console.log( test() );                    // null
console.log( test({}) );                  // null
console.log( test({ birthdate: [] }) );   // null
console.log( test({ birthdate: [42] }) ); // 42
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

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