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> `}
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.
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.
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.
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.
.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('')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With