0

I have an array

let availabilityArray = [currentYearAvailability, previousYearAvailability, previousLastYearAvailability]
console.log(availabilityArray)

when I Console, I can able to find what is available what is not available

const preAuthAvailabilityYears =
    [
      currentYearAvailability ? currentYear : undefined,
      previousYearAvailability ? previousYear : undefined,
      previousLastYearAvailability ? previousLastYear : undefined,
    ]

Console

I need only the availability years to be displayed in the array instead showing as undefined as third argument, If it is undefined i shouldnot should in array

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
Siva Sai
  • 319
  • 2
  • 5
  • 21
  • 1
    Does this answer your question? [How to define an array with conditional elements?](https://stackoverflow.com/questions/44908159/how-to-define-an-array-with-conditional-elements) – lusc Aug 15 '22 at 12:24

2 Answers2

0
const preAuthAvailabilityYears =
    [
      currentYearAvailability ? currentYear : undefined,
      previousYearAvailability ? previousYear : undefined,
      previousLastYearAvailability ? previousLastYear : undefined,
    ].filter(Boolean)
Konrad
  • 21,590
  • 4
  • 28
  • 64
0

You can filter it out like that if you are not required undefined.

Option 1

const preAuthAvailabilityYears =
    [
      currentYearAvailability ? currentYear : undefined,
      previousYearAvailability ? previousYear : undefined,
      previousLastYearAvailability ? previousLastYear : undefined,
    ].filter(Boolean);

Option 2

const preAuthAvailabilityYears =
        [
          currentYearAvailability ? currentYear : undefined,
          previousYearAvailability ? previousYear : undefined,
          previousLastYearAvailability ? previousLastYear : undefined,
        ].filter( item => item);
Sonu Sindhu
  • 1,752
  • 15
  • 25