Javascript Javascript如何在新行上显示数组的每个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10982913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript how to show each element of array on a new line
提问by Leron_says_get_back_Monica
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:
我有一个字符串构建形式的逗号分隔值,我split
用来获取每个值,然后我想在新行上显示每个值,但真正发生的是我在新行上获取每个值,除了显示的最后两个值一起在同一条线上。只想说清楚:
value1
,value2
,value3
,value4,value5
值1
,值2
,值3
,值4,值5
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.
只是要清楚这是用 ExtJS 4 编写的,我几乎可以肯定,在这种情况下,问题是纯 JavaScript 并且与 ExtJS 4 无关,但无论如何,也许我错了。
So any ideas why does it happen and how I could make that last elemnt to get on a new line as well?
那么有什么想法为什么会发生这种情况,以及我如何使最后一个元素也能进入新的生产线?
Thanks
谢谢
Leron
勒龙
采纳答案by Stefan
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 循环是可疑的。首先,您没有处理所有项目(最后一个丢失了,正如@sarfraz 指出的那样)。其次,您将zzz
在 for 循环体中返回结果 ( ):
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:
在 JavaScript 中,您可以简单地“加入”数组:
return dates.split(',').join("<br />")
Since you are simply replacing strings you could use the replace
method:
由于您只是替换字符串,因此可以使用以下replace
方法:
return dates.replace(",", "<br />");
回答by VIJAY P
i have modified your function bit cleaner.since already stefan mentioned your mistake.
我已经修改了你的函数有点清洁。因为 stefan 已经提到了你的错误。
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"));
回答by DevAelius
For the React-Native
对于 React-Native
const your_array_name = [
{
id: 1,
text: 'Lorem ipsum is simple dummy text for printing the line',
skills: ['javascript', 'java']
},
{
id: 2,
text: 'Lorem ipsum is simple dummy text.',
skills: ['javascript', 'java']
} ]
`<Text style={{color:'#ff8500',fontSize:18}}>{your_array_name.skills.splice(',').join("\n")}</Text>`