i have an array which contains different values, i want split them off and print in browser. but i want alert it first value. i have made a function but it is not wokring
<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>
6 Answers 6
You can't split an array. It is already split.
You might be looking for something like this:
var foo = ['a', 'b', 'c'];
console.log(foo.shift()); // Outputs "a"
console.log(foo.join(',')); // Outputs "b,c"
... but it hard to tell what your goals are from your question.
Comments
$(function(){
var jam= new Array("first","second","third","fourth");
var Klnew= jam.slice();
// for(i=0;i<Klnew.length;i++) {}
alert(Klnew[0])
})
Comments
var jam= new Array("first","second","third","fourth");
alert(jam[0]); // alert before the join.
var Klnew= jam.join(","); // join, not split the array...
Output:
"first,second,third,fourth"
You would use split for getting an array out of a string:
"first,second,third,fourth".split(',') ==> ['first', 'second', 'third', 'fourth']
Comments
split (of string) method splits string to array.
join (of array) method joins array elements into string.
So:
arr = Array("first","second","third","fourth");
str = arr.join(",")
alert(str) // will alert "first,second,third,fourth"
alert(arr == str.split(","))// will alert true
splitis the function ofString,not ofArray.