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

Change array elements

There is an array

[ [ 'Alex', '167%' ],
  [ 'Benjamin', '127%' ],
  [ 'Elijah', '117%' ],
  [ 'Liam', '136%' ],
  [ 'Theodore', '135%' ],
  [ 'Mia', '128%' ] ]

I need to make it look like this

[ 'Alex                     167%',
  'Benjamin                 127%',
  'Elijah                   117%',
  'Liam                     136%',
  'Theodore                 135%',
  'Mia                      128%' ]

I need the elements of each nested array to be combined into a string and such a number of spaces are inserted between them so that the length of this string is 29 characters

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 could use a map combined with padEnd

const items = [
  ['Alex', '167%'],
  ['Benjamin', '127%'],
  ['Elijah', '117%'],
  ['Liam', '136%'],
  ['Theodore', '135%'],
  ['Mia', '128%']
];


const combined = items.map(([name, percent]) =>
  name.padEnd(29 - percent.length, ' ') + percent
);

console.log(combined);
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