Array对象

    科技2022-07-10  162

    创建 Array 对象的语法:

    new Array(); new Array(size); new Array(element0, element1, ..., elementn);

    参数

    参数 size 是期望的数组元素个数。返回的数组,length 字段将被设为 size 的值。

    参数 element …, elementn 是参数列表。当使用这些参数来调用构造函数 Array() 时,新创建的数组的元素就会被初始化为这些值。它的 length 字段也会被设置为参数的个数。

    Array 对象属性

    constructor()

    返回对创建此对象的数组函数的引用。

    <script type="text/javascript"> function employee(name,job,born) { this.name=name; this.job=job; this.born=born; } var bill=new employee("Bill Gates","Engineer",1985); document.write(bill.constructor); </script>//function employee(name, job, born){this.name = name; this.job = job; this.born = born;}
    length()

    设置或返回数组中元素的数目。

    <script type="text/javascript"> var arr = new Array(3) arr[0] = "John" arr[1] = "Andy" arr[2] = "Wendy" document.write("Original length: " + arr.length) document.write("<br />") arr.length=5 document.write("New length: " + arr.length) </script>

    输出

    Original length: 3 New length: 5
    prototype()

    使您有能力向对象添加属性和方法。

    <script type="text/javascript"> function employee(name,job,born) { this.name=name; this.job=job; this.born=born; } var bill=new employee("Bill Gates","Engineer",1985); employee.prototype.salary=null; bill.salary=20000; document.write(bill.salary); </script>//20000

    Array 对象方法

    concat()

    连接两个或更多的数组,并返回结果。

    <script type="text/javascript"> var a = [1,2,3]; document.write(a.concat(4,5)); </script>//1,2,3,4,5 <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" var arr2 = new Array(3) arr2[0] = "James" arr2[1] = "Adrew" arr2[2] = "Martin" var arr3 = new Array(2) arr3[0] = "William" arr3[1] = "Franklin" document.write(arr.concat(arr2,arr3)) </script>//George,John,Thomas,James,Adrew,Martin,William,Franklin
    join()

    把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔。默认‘,’号

    <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr.join(‘.’)) </script>//George.John.Thomas
    pop()

    删除并返回数组的最后一个元素

    <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr) document.write("<br />") document.write(arr.pop()) document.write("<br />") document.write(arr) </script>//George,John,Thomas Thomas George,John
    push()

    向数组的末尾添加一个或更多元素,并返回新的长度。

    <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write(arr.push("James") + "<br />") document.write(arr) </script>//George,John,Thomas 4 George,John,Thomas,James
    reverse()

    颠倒数组中元素的顺序。

    <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write(arr.reverse()) </script>//George,John,Thomas Thomas,John,George
    shift()

    把数组的第一个元素从其中删除,并返回第一个元素的值。

    <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write(arr.shift() + "<br />") document.write(arr) </script>//George,John,Thomas George John,Thomas
    slice()

    从某个已有的数组返回选定的元素

    <script type="text/javascript"> var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" document.write(arr + "<br />") document.write(arr.slice(2,4) + "<br />") document.write(arr) </script>//George,John,Thomas,James,Adrew,Martin Thomas,James George,John,Thomas,James,Adrew,Martin
    splice()

    删除元素,并向数组添加新元素。

    //我们从第二个元素开始,到第二个元素后n个元素删除 并添加一个元素上去 <script type="text/javascript"> var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" document.write(arr + "<br />") arr.splice(2,0,"William") document.write(arr + "<br />") </script>//George,John,Thomas,James,Adrew,Martin George,John,William,Thomas,James,Adrew,Martin
    sort()

    对数组的元素进行排序

    <script type="text/javascript"> var arr = new Array(6) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" arr[3] = "James" arr[4] = "Adrew" arr[5] = "Martin" document.write(arr + "<br />") document.write(arr.sort()) </script> //George,John,Thomas,James,Adrew,Martin Adrew,George,James,John,Martin,Thomas <script type="text/javascript"> var arr = new Array(6) arr[0] = "10" arr[1] = "5" arr[2] = "40" arr[3] = "25" arr[4] = "1000" arr[5] = "1" document.write(arr + "<br />") document.write(arr.sort()) </script> //10,5,40,25,1000,1 1,10,1000,25,40,5 <script type="text/javascript"> function sortNumber(a,b) { return a - b } var arr = new Array(6) arr[0] = "10" arr[1] = "5" arr[2] = "40" arr[3] = "25" arr[4] = "1000" arr[5] = "1" document.write(arr + "<br />") document.write(arr.sort(sortNumber)) </script> //10,5,40,25,1000,1 1,5,10,25,40,1000
    toSource()

    返回该对象的源代码

    <script type="text/javascript"> function employee(name,job,born) { this.name=name; this.job=job; this.born=born; } var bill=new employee("Bill Gates","Engineer",1985); document.write(bill.toSource()); </script>//({name:"Bill Gates", job:"Engineer", born:1985})
    toString()

    可把数组转换为字符串,并返回结果。

    <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr.toString()) </script>//George,John,Thomas
    toLocaleString()

    把数组转换为本地字符串。

    <script type="text/javascript"> var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr.toLocaleString()) </script>//George, John, Thomas
    unshift()

    向数组的开头添加一个或更多元素,并返回新的长度。

    <script type="text/javascript"> var arr = new Array() arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr + "<br />") document.write(arr.unshift("William") + "<br />") document.write(arr) </script>//George,John,Thomas 4 William,George,John,Thomas
    valueOf()

    返回数组对象的原始值

    Processed: 0.069, SQL: 8