0129 JavaScript array: concept, create an array, to obtain elements in the array

The concept 1.1 array

  • The arrays can be stored together with a group of related data, and provides easy access (acquisition) mode.
  • An array is a collection of data , each of which is referred to as data elements in the array can store any type of element . Elegant way the array is a set of data stored in a single variable name.

1.2 Creating an array

JS create an array of two ways:

  • Creating an array using the new

    var 数组名 = new Array() ;
    var arr = new Array();   // 创建一个新的空数组

    Note Array (), A should be capitalized

  • Creating an array using the array literal

    //1. 使用数组字面量方式创建空的数组
    var  数组名 = [];
    //2. 使用数组字面量方式创建带初始值的数组
    var  数组名 = ['小白','小黑','大黄','瑞奇'];
    • Literal array is square brackets []
    • Declare an array called array initialization and assignment
    • This way a literal way but also our future use of the most
  • The type of array elements

    Array can store any type of data, such as strings, numbers, Boolean and so on.

    var arrStus = ['小白',12,true,28.9];

1.3 Gets elements in the array

Index (subscript): sequence number is used to access the array element (array index starts from 0).

Array may be accessed through an index set, modify the corresponding array element, the array element may be acquired by "name of the array [index]" form.

// 定义数组
var arrStus = [1,2,3];
// 获取数组中的第2个元素
alert(arrStus[1]);    

Note: If the value does not access the array and index values ​​corresponding element, the resultant is undefined

        // 1.数组(Array) :就是一组数据的集合 存储在单个变量下的优雅方式 
        // 2. 利用new 创建数组
        var arr = new Array(); // 创建了一个空的数组
        // 3. 利用数组字面量创建数组 []
        var arr = []; // 创建了一个空的数组
        var arr1 = [1, 2, '哈哈', true];
        // 4. 我们数组里面的数据一定用逗号分隔
        // 5. 数组里面的数据 比如1,2, 我们称为数组元素
        // 6. 获取数组元素  格式 数组名[索引号]  索引号从 0开始 
        console.log(arr1);
        console.log(arr1[2]); // 哈哈
        console.log(arr1[3]); // true
        var arr2 = ['迪丽热巴', '古丽扎娜', '佟丽丫丫'];
        console.log(arr2[0]);
        console.log(arr2[1]);
        console.log(arr2[2]);
        console.log(arr2[3]); // 因为没有这个数组元素 所以输出的结果是 undefined

Guess you like

Origin www.cnblogs.com/jianjie/p/12148435.html