我有一个包含不同值的数组,我希望将它们拆分并在浏览器中打印。但我想提醒它第一个价值。我已经完成了一项功能,但它没有做到这一点
<script type="text/javascript">
$(function(){
var jam= new Array("first","second","third","fourth");
var Klnew= jam.split(",");
//for(i=0;i<Klnew.length;i++) {}
alert(Klnew[0])
});
</script>
答案 0 :(得分:7)
您无法拆分数组。它已经分裂了。
你可能正在寻找这样的东西:
var foo = ['a', 'b', 'c'];
console.log(foo.shift()); // Outputs "a"
console.log(foo.join(',')); // Outputs "b,c"
...但很难说出你的问题是什么目标。
答案 1 :(得分:1)
var jam= new Array("first","second","third","fourth");
alert(jam[0]); // alert before the join.
var Klnew= jam.join(","); // join, not split the array...
<强>输出:强>
"first,second,third,fourth"
答案 2 :(得分:1)
$(function(){
var jam= new Array("first","second","third","fourth");
var Klnew= jam.slice();
// for(i=0;i<Klnew.length;i++) {}
alert(Klnew[0])
})
答案 3 :(得分:0)
您可以使用split从字符串中获取数组:
“第一,第二,第三,第四”.split(',')==&gt; ['第一','第二','第三','第四']
答案 4 :(得分:0)
即使这样也会起作用 -
var jam = new Array("first", "second", "third", "fourth");
alert(jam[0]);
答案 5 :(得分:0)
split
(of string)方法将字符串拆分为数组。
join
(数组)方法将数组元素连接到字符串中。
所以:
arr = Array("first","second","third","fourth");
str = arr.join(",")
alert(str) // will alert "first,second,third,fourth"
alert(arr == str.split(","))// will alert true