排序数组而不显示逗号 [英] Sort array without displaying commas
问题描述
我是最精通计算机的人完成任务"的完美案例. 在我正在创建的网站上,我需要对大量名称进行排序.名称经常更改,很多人都在更改它们.名称的数量也会发生变化,因此按数字编制索引也不是一件好事.
I'm the perfect case of the "most computer savvy guy gets the task." On the website I'm making, I need to sort a large number of names. The names change often, and lots of people change them. The number of names also change, so indexing by number would also not be a good thing.
我发现的示例代码如下:
My sample code I found looks like this:
<script type="text/javascript">
var fruits = ["Banana<br />", "Orange<br />", "Apple<br />", "Mango<br />",];
document.write(fruits.sort());
</script>
除逗号显示在网站上外,此方法有效.这是不可接受的.我正在寻找一种使逗号在显示时离开网站的方法.
This works with the exception that the commas are displayed on the website. This isn't acceptable. I'm looking for a way to make the commas go away from the website when it's displayed.
推荐答案
数组不是字符串,转换数组的默认方法是通过,
连接元素.只需指定您自己的连接字符串即可:
An array isn't a string, and the default way of converting it is to join the elements by ,
. Just specify your own joining string instead:
var fruits = ["Banana<br />", "Orange<br />", "Apple<br />", "Mango<br />",];
document.write(fruits.sort().join('')); // Don't join by anything
这篇关于排序数组而不显示逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!