Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!