Javascript Array object, basic

Javascript Array object, basic

1. What is an array?

The function of the array object is to use a single variable to store a series of values.

2. Create an array and assign values ​​to it in three ways

//第一种方式:
var name=new Array();//声明一个变量给他赋值为一个数组
name[0]="小明";
name[1]="小月";
name[2]="小雨";
name[3]="小风";
//第二种方式
var name=new Array("小明","小月","小雨","小风")
//第三种方式
var name=["小明","小月","小雨","小风"]

3. Access the array

Each element in the array has a subscript, and the subscript starts from zero. The first element of the array is subscript 0, the second element is 1, the third is 2, and so on.

var name=new Array();//声明一个变量给他赋值为一个数组
name[0]="小明";
name[1]="小月";
name[2]="小雨";
name[3]="小风";
//查找数组里的第一个元素写到html页面
document.write(name[0])
//访问数组里元素的方法,大致的意思就是:定义的数组变量名加上[]中括号里边写想要得到的元素的下标。

4. Array methods and properties

  • length calculates the length of the array

    usage:

    var name=["小明","小月","小风"];
    //计算数组的长度写到html页面
    document.write(name.length)
    //数组名.length得到的值就是数组的长度
  • indexof query element index number in the array

    usage:

    var name=["小明","小月","小风"];
    //查询小风这个这个元素的索引值是多少结果写到html页面
    document.write(name.indexof("小风"));
    //数组名.indexof("需要查询的元素")得到的就是所要查询元素的索引值。
    

Guess you like

Origin blog.csdn.net/weixin_46475440/article/details/108726906