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 join the elements of an array such that the first letter is capital for all elements in JavaScript?

I want to join the elements of an array separated by commas and the first letter of each element to be capital, this should not affect the object values itself.

For example:

obj= ["apple","orange","lemon"]

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

Output should be:
Apple, Orange, Lemon.

Here is what I have tried and failed with:

obj.charAt(0).toUpperCase().join(', ')

>Solution :

Here is the code:

let a = ['apple', 'orange', 'lemon'];
let res = a.map(fruit => fruit.charAt(0).toUpperCase() + fruit.slice(1));
console.log(res); // [ "Apple", "Orange", "Lemon" ]
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