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

Split dates in array with dash (-) and take first number from split result

I have one array with dates having format as a DD-MM-YYYY. I have to take only DD i.e. days from it and push in another array.

I am trying using split function to separate out it but not able to push into array.

  let arrayData = ["15-07-2022", "18-07-2022", "20-07-2022"];
  
  let pushData = [];
        arrayData.map(x => (
            pushData.push(x.split('-'))
     ))
     
     //output should be [15,18,20]

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

>Solution :

You can use Array.map() to achieve the desired result. Note I use + to convert the string to numbers as shown in your expected output.

let arrayData = ["15-07-2022", "18-07-2022", "20-07-2022"];
const result = arrayData.map((date) => +date.split('-')[0])
console.log(result)
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