js 数组定义

时间:2013-08-18 08:10:01  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:element1, element2, ..., elementN];    // In both cases the arr variable is an array that has three element: A, B, C var arr = new Array ("A", "B", "C");     // or var arr = ["A", "B", "C"];     // you can access the elements of...
  正文:

js(第二电脑网http://www.002pc.com) 数组定义
国外网站搞下来的一些关于js(第二电脑网http://www.002pc.com)数组操作实现 ,本教程主要是定义这方面的。
var arr = new Array ();
var arr = new Array ([length]);
var arr = [element1, element2, ..., elementN];

   // In both cases the arr variable is an array that has three element: A, B, C
var arr = new Array ("A", "B", "C");
    // or
var arr = ["A", "B", "C"];

    // you can access the elements of the array by zero-based indices
var firstItem = arr[0];     // "A"
var thirdItem = arr[2];     // "C"

    // you can get the length of the array by the length property
var len = arr.length;       // 3

实例二

var arr1 = new Array ("A", "B", "C");
var arr2 = new Array (1, 2, 3);
var multiArr = new Array (arr1, arr2);
    // or
var multiArr = [arr1, arr2];
    // or
var multiArr = [["A", "B", "C"], [1, 2, 3]];

    // you can access the elements of the array by zero-based indices
var firstRow = multiArr[0];     // same as arr1
var secondRowFirstCell = multiArr[1][0]; // 1


var arr = ["A", "B", "C"];

    // display the elements of the array (ABC)
for (var i = 0; i < arr.length; i++) {
    document.write (arr[i]);
}

    // iterating with the for in statement, i will be 0, 1, 2.
    // The output will be ABC
for (var i in arr) {
    document.write (arr[i]);
}

 // First we must create an array:
var arr = [];
    // Add elements at the first position:
arr[0] = "A";
    // Add elements at the second position:
arr[1] = "B";
    // Adding an element to the end of the array:
arr.push ("C");
    // Now the arr variable is an array that have three elements: A,B,C.

var arr1 = ["A", "B", "C"];
var arr2 = [1, 2, 3];
    // Concat the arrays:
var newArr = arr1.concat (arr2);

var arr = ["A", "B", "C"];
document.write (arr);   // output: A,B,C

arr.splice (1, 1);
document.write ("<br />");
document.write (arr);   // output: A,C


var arr = {};
    // You can add elements to an associative array in two different ways:
arr["name"] = "John";
arr["location"] = "Utah";
    // second way
arr.age = 13;

    // you can access the elements of this array by string indices
var loc = arr["location"];  // "Utah"
    // or like a member
var loc = arr.location;     // "Utah"

    // you cannot get the length of the array via the length property,
    // it contains an invalid value
var len = arr.length;       // len will


var arr = {};
arr["name"] = "John";
arr["location"] = "Utah";
arr["age"] = 13;

    // iterating with the for in statement, i will be 'name', 'location', 'age'.
    // The output will be JohnUtah13
for (var i in arr) {

js 数组定义》由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Programming/JavaScript/26969.html


关于《js 数组定义》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td