Skip to content Skip to sidebar Skip to footer

How To Add A Dot Before Each Item In This List Of Array?

I have an array like this: const array = ['jpg', 'png]. Right now I can join them like this: array.join(',') to produce: jpg,png What the simplest way of getting something like thi

Solution 1:

You can chain .map and .join methods:

array.map(function (fileExtension) {
    return'.' + fileExtension;
}).join(',');

Fat arrow notation is also acceptable (and short!)

array.map(extension => '.' + extension).join(',');

Solution 2:

const array = ['jpg', 'png'];
const extensions = array.map((item) =>`.${item}`).join(',');

console.log(extensions);

Post a Comment for "How To Add A Dot Before Each Item In This List Of Array?"