function InitArray()
{
var spreadsheet = [['A1', 'B1', 'C1', 'D1'], ['A2', 'B2', 'C2', 'D2'], ['A3', 'B3', 'C3', 'D3'], ['A4', 'B4', 'C4', 'D4']];
var col2 = spreadsheet[1];//hang
alert(col2[2]);//lie
alert(spreadsheet[1][0]);
}
var students = ['Sam', 'Joe', 'Sue', 'Beth'];
students[4] = 'Mike';
students[students.length] = 'Sarah';
students.push('Steve');
var students = ['Sam', 'Joe', 'Sue', 'Beth', 'Mike', 'Sarah', 'Steve'];
students.splice(4, 1);//remove begin with 4
var myString = 'apples are good for your health';
var myArray = myString.split('a'); // we break myString apart on every 'a' found.
alert(myArray.join(', ')); // we join myArray back together with a comma so you can see each item
alert(myArray.join('a')); // now we join myArray back together with an 'a' so we get our original string back
// "pop" removes the last item from an array and returns it. "shift" removes the first item from an array and returns it.
var students = ['Sam', 'Joe', 'Sue', 'Beth'];while(students.length>0){ alert(students.pop());}
If all you want to do is empty your array, a quick way to do it is to simply set the length to 0:
students.length = 0All of the arrays we have been working with are called "Indexed Arrays" since each item in the array has an index that you must use to access it. There are also "Associative Arrays". An Associative Array is a fancy term meaning that each item in the array is accessed with a name as opposed to an index:
var grades = [];Associative Arrays act a little differently than Indexed Arrays do. For starters, the length of your array will be 0 in this example. So how do we see what items are in our array? Well the only way to do this is with a "for-in" loop:grades['Sam'] = 90;
grades['Joe'] = 85;
grades['Sue'] = 94;
grades['Beth'] = 82;for(student in grades){
alert(student + "'s grade is: " + grades[student]);
}The final thing to note about arrays is that you can actually combine Associative and Indexed Arrays, although this is not usually reccomended because it can cause some confusion. If done properly, however, you can get the best of both worlds:
var students = ['Sam', 'Joe', 'Sue', 'Beth'];students['Sam'] = 90;
students['Joe'] = 85;
students['Sue'] = 94;
students['Beth'] = 82;alert('There are '+(students.length)+' students: '+students.join(', '));
for(var i=0; i<students.length; i++){
alert(students[i]+"'s grade is: "+students[students[i]]);
}

浙公网安备 33010602011771号