最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Join() an array by tab? - Stack Overflow

programmeradmin1浏览0评论

I'm attempting to integrate with a legacy system which depends on a value being sent via a GET request as a tab delimited string. I have my data in an array, but am having an issue trying to join() it in the correct manner.

Here's is what I've attempted, none of which works;

var myArray = ["a", "b", "c"]

myArray.join(\t);
myArray.join(/\t/);
myArray.join('\t');
myArray.join('\\t');
myArray.join('   '); // tab character

Edit

It appears the actual issue appears that joining by \t does indeed work, however when it is URL encoded the tab is not being turned into %09 as it should be, but is instead removed.

Desired URL encoded var:

"?tags=a%09b%09c"

Actual output:

"?tags=abc"

Does anyone know how to solve this?

I'm attempting to integrate with a legacy system which depends on a value being sent via a GET request as a tab delimited string. I have my data in an array, but am having an issue trying to join() it in the correct manner.

Here's is what I've attempted, none of which works;

var myArray = ["a", "b", "c"]

myArray.join(\t);
myArray.join(/\t/);
myArray.join('\t');
myArray.join('\\t');
myArray.join('   '); // tab character

Edit

It appears the actual issue appears that joining by \t does indeed work, however when it is URL encoded the tab is not being turned into %09 as it should be, but is instead removed.

Desired URL encoded var:

"?tags=a%09b%09c"

Actual output:

"?tags=abc"

Does anyone know how to solve this?

Share Improve this question edited Nov 30, 2018 at 7:57 Rory McCrossan asked Apr 19, 2013 at 13:21 Rory McCrossanRory McCrossan 338k41 gold badges320 silver badges351 bronze badges 1
  • How do you show the output? maybe you just need  ? And what's wrong with: myArray.join(' ');? – gdoron Commented Apr 19, 2013 at 13:23
Add a ment  | 

3 Answers 3

Reset to default 6

This one works on my chrome console :

var myArray = ["a", "b", "c"];

console.log(myArray.join('\t'));

See on this jsfiddle.

This one is working too :

myArray.join('   '); // tab character

Browser : Chrome 26.0.1410.65 (os x)

Edit :

var myArray = ["a", "b", "c"];

var string = myArray.join('\t');

var url = encodeURIComponent(string);

console.log(url); //output : a%09b%09c 

http://jsfiddle/mGZdk/2/

Your problem is that you aren't assigning it to a new variable. Join() does not modify the original variable. Try this:

var myArray = ["a", "b", "c"]

var array_string = myArray.join('\t');

alert(array_string);

\t should work...

var arr = [1, 2, 3];
var str = arr.join("\t");

However, how are you displaying your output? If you are trying to display the content on a normal HTML page, the tabs will appear like spaces. If you put the contents in special elements like <pre>, they should appear normally.

I know it's a nasty solution, but you can use multiple &nbsp; instead...

var arr = [1, 2, 3];
var str = arr.join("&nbsp;&nbsp;&nbsp;&nbsp;"); // s-sorry ;__;
发布评论

评论列表(0)

  1. 暂无评论