Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript how to show each element of array on a new line

Tags:

I have a string build form comma separated values I use split to get each value and after that I want to show each value on a new line but what really happens is that I get each value on a new line except of the last two which are shown together on a same line. Just to make it clear:

value1

,value2

,value3

,value4,value5

Here is the function which I'm using:

_checkDates: function(dates) {     if (dates != null)     {         var zzz = dates.split(',');         var xxx = zzz.length;         console.log(xxx);         for (var i=0; i<=xxx; i++)         {             zzz[i] = zzz[i] + '<br />';             return zzz;         }     }     return dates; } 

Just to be clear this is written in ExtJS 4, I'm almost sure that in this case the problem is pure JavaScript and is not related with ExtJS 4 but anyways, maybe I'm wrong.

So any ideas why does it happen and how I could make that last element to get on a new line as well?

like image 548
Leron_says_get_back_Monica Avatar asked Jun 11 '12 15:06

Leron_says_get_back_Monica


People also ask

How can you print each item of this array on a separate line?

To print the array elements on a separate line, we can use the printf command with the %s format specifier and newline character \n in Bash. @$ expands the each element in the array as a separate argument. %s is a format specifier for a string that adds a placeholder to the array element.

How do you join an array with a line break?

Convert array to string with newlines using join()The join('\r\n') method is used. '\r\n' is passed in as the method's argument that acts as a separator while converting the array to string values. The array elements are divided into newline using '\r\n'.

How do you split an array in JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do I see all the elements in an array?

The every() method executes a function for each array element. The every() method returns true if the function returns true for all elements.


2 Answers

i have modified your function bit cleaner.since already stefan mentioned your mistake.

function splitDate(dates) {     if (dates != null)     {         var dates = dates.split(',');         var xxx = dates.length;         console.log(xxx);         for (var i=0; i<xxx; i++)         {             dates[i] = dates[i];                             }     }     console.log(dates.join('\r\n'));     return dates.join('\r\n');         } 

the above function you can do it in a single line:

if it's an array you can split into new line in following way:

var arr = ['apple','banana','mango']; console.log(arr.join('\r\n')); 

if it's a string:

var str = "apple,banana,mango"; console.log(str.split(',').join("\r\n")); 
like image 54
VIJAY P Avatar answered Oct 09 '22 19:10

VIJAY P


The for-loop is suspicious. Firstly, you do not process all items (the last one is missing, as @sarfraz pointed out). Sencondly you are returning the result (zzz) in the for-loop body:

for (var i=0; i<=xxx; i++) {   zzz[i] = zzz[i] + '<br />';   return zzz; // for-loop will stop here! resulting in ["value1<br />", "Value2", etc...] } 

In Javscript you can simple "join" the array:

return dates.split(',').join("<br />") 

Since you are simply replacing strings you could use the replace method:

return dates.replace(",", "<br />"); 
like image 22
Stefan Avatar answered Oct 09 '22 20:10

Stefan