Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 map array to string without comma

Tags:

I am trying to map a list of errors into a list that I will show in a tooltip. The list added, but there's a , added between every li element.

I guess this is because this basically does errors.map(error => ...).toString() in the end. Any ideas how I can map the strings in errors array without having this comma added?

data-tip = {`   Precisa de corrigir os seguintes problemas antes de publicar o anúncio:</br>   <ul>     ${errors.map(error => `<li>${error}</li>`)}   </ul> `} 
like image 350
Fábio Santos Avatar asked Jul 05 '17 08:07

Fábio Santos


People also ask

How do you remove commas from an array?

You can use the join() method in JavaScript to remove commas from an array. The comma delimiter in an array works as the separator. For example, let arr = ['alpha', 'bravo', 'charlie']. Using the join() method, you can remove or replace all the commas with a space.

How do you turn an array into a string?

In order to convert an array into a string in Javascript, we simply apply the toString() method on the given array, and we get a stringified version of our array. Internally javascript first converts each element into string and then concretes them to return the final string.

How do I turn an array into a string with spaces?

To convert an array to a string with spaces, call the join() method on the array, passing it a string containing a space as a parameter - arr. join(' ') . The join method returns a string with all array elements joined by the provided separator.

Is a map a 2d array?

Map Method for 2-dimensional Arrays Also called a map within a map: Sometimes you'll come across a multidimensional array -- that is, an array with nested arrays inside of it.


1 Answers

.toString() of the array object uses Array.prototype.join method for converting the array into a string. The .join method by default uses , for joining the elements. You can replace the .toString() with .join('').

like image 187
undefined Avatar answered Sep 30 '22 09:09

undefined