-6

I have an array of some parameters like:

arr=['abc','xyz','how are you, I am good'];

I want a result like:

  1. abc
  2. xyz
  3. how are you, I am good

Using javascript only.

I am explaining again something was wrong in last question. Here is the scenario.

I have 6 string varables having some special charecters.

I assigned these 6 strings to an array like this:

arr=[str1,str2,str3.....);

After that I am doing:

arr=escape(arr); 

Because some string variable have special charecters.

After that I am reviving this arr into other function like this.

var newarr=unescape(arr);

Now I want to get all six variables from this new arr, how can I do this?

Currently I am using the split function like.

split(',');

But the problem is in this some variables have, inside so it splits twice I don't want that.

Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
vikas
  • 336
  • 2
  • 5
  • 16
  • 4
    it's not clear: do you also want numeric indexes aside? (1., 2...)? *"using javascript only"* : jQuery can be used or not? – Fabrizio Calderan Sep 11 '12 at 13:47
  • 4
    Looking at the orginal vs the 6 other edits, it is totally different. How is this final string supposed to look like? – epascarello Sep 11 '12 at 13:48
  • var txt = ''; for(var i = 0, len = arr.length; i < len; i++) { txt += i + '. ' + arr[i] + '
    '; } document.write(txt);
    – Omri Sep 11 '12 at 13:49
  • Do you want to create an `
      ` with the contents of that array?
    – João Silva Sep 11 '12 at 13:50
  • i dont want 1 2 3 maintain idex – vikas Sep 11 '12 at 13:52
  • i want only 3 strings so that i can pick arr[0], arr[1] like that – vikas Sep 11 '12 at 13:52
  • @user1650946 You already have 3 strings in the array and you may pick them with `arr[0]`, `arr[1]`, etc. – VisioN Sep 11 '12 at 13:53
  • var list = document.createElement('ol'), li, txt; for(var i = 0, len = arr.length; i < len; i++){ li = document.createElement('li'); txt = document.createTextNode(arr[i]); li.appendChild(txt); } document.appendChild(list); – Omri Sep 11 '12 at 13:53
  • possible duplicate of [how to split a string in javascript](http://stackoverflow.com/questions/1493407/how-to-split-a-string-in-javascript) – LittleBobbyTables - Au Revoir Sep 11 '12 at 19:42

6 Answers6

8

Use join(), like this:

arr.join(" ");

Here " " is a clue.

You man use "<br />" to join array elements with the new line.

VisioN
  • 143,310
  • 32
  • 282
  • 281
1

Umm you can just get them out of the array without split or join

var arr=['abc','xyz','how are you, I am good'];
console.log('1. ' + arr[0]);
console.log('2. ' + arr[1]);
console.log('3. ' + arr[2]);

http://jsfiddle.net/8hGxM/

wirey00
  • 33,517
  • 7
  • 54
  • 65
0

Use join function : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/join

arr.join(' ');
odupont
  • 1,946
  • 13
  • 16
0

Use split function

string.split(',');
AboQutiesh
  • 1,696
  • 2
  • 9
  • 14
0

Try with

string.split(',');

You will get it

GautamD31
  • 28,552
  • 10
  • 64
  • 85
0

Fabrizio, this worked for me:

var arr=['abc','xyz','how are you, I am good'];
var res = arr.join("\n");
console.log(res);
Omeriko
  • 83
  • 1
  • 10