Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Map Through Array Condiotionally

I am wondering, from the research I have done I have not found anything, but is there a way to conditionally map through an array of objects. For instance, I have an array of objects for doctors appointments and I am only wanting to map through the doctors appointments where the date has not past.

I normally do this, but this is not adding in any conditions.

{doctorsApptData.map(apptData => (
   <div>This date is valid! {apptData.date}</div>
))}

I have tried something like this, but no luck

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{doctorsApptData.map(apptData => (
apptData.date >= DateTime.now() && (
   <div>This date is valid! {apptData.date}</div>
)))}

Thanks!

>Solution :

You can use filter method before map. This method creates a new array with only the elements that meet a certain condition.

For example:

{
  doctorsApptData
    .filter((apptData) => apptData.date >= DateTime.now())
    .map((apptData) => <div>This date is valid! {apptData.date}</div>);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading