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

How to modify multidimensional array?

I have this array

[[11,12,13,14],[21,22,23,24],[31,32,33,34],[41],[43],[51]]

,

expected output like this: 11-14,21-24,31-34,41,43,51

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 :

We can do it via Array.map()

const data = [[11,12,13,14],[21,22,23,24],[31,32,33,34],[41],[43],[51]]

let result = data.map(d => {
   if(d.length < 2){
     return d[0]
   }
   return d.at(0) +' -' + d.at(-1)
})
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