0

我有一个包含不同值的数组,我想将它们拆分并在浏览器中打印。但我想提醒它第一个值。我做了一个功能,但它不是工作

<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>
4

6 回答 6

7

您不能拆分数组。它已经分裂了。

你可能正在寻找这样的东西:

var foo = ['a', 'b', 'c']; 
console.log(foo.shift());    // Outputs "a"
console.log(foo.join(','));  // Outputs "b,c"

......但很难从你的问题中看出你的目标是什么。

于 2012-06-14T06:40:44.173 回答
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"
于 2012-06-14T06:39:09.347 回答
1
   $(function(){
        var jam= new Array("first","second","third","fourth");
        var Klnew= jam.slice();
    //  for(i=0;i<Klnew.length;i++) {}
        alert(Klnew[0])
   })
于 2012-06-14T06:40:11.927 回答
0

您将使用 split 从字符串中获取数组:

"第一,第二,第三,第四".split(',') ==> ['第一','第二','第三','第四']

于 2012-06-14T06:42:21.953 回答
0

即使这样也行得通——

var jam = new Array("first", "second", "third", "fourth");
alert(jam[0]);  
于 2012-06-14T06:44:17.777 回答
0

split(of string) 方法将字符串拆分为数组。

join(of array) 方法将数组元素连接成字符串。所以:

arr = Array("first","second","third","fourth");
str = arr.join(",")
alert(str) // will alert "first,second,third,fourth"
alert(arr == str.split(","))// will alert true
于 2012-06-14T06:45:39.457 回答