js的Array类型

•长度可变数组对象
–0-based
–最大长度2^32−1 = 4294967295
•构造Array对象
–new Array():空数组
–new Array(3):长度为3的数组
–new Array(1, "Hello"):构造有两个元素的数组


Array.prototype.
•length属性:数组长度,可读写
•toString()/toLocaleString()方法:
–返回逗号分割的字符串
–两者区别是得到每个对象字符串的方式
•concat([ item1 [ , item2 [ , … ] ] ])方法:
–返回一个新数组,保存了原数组所有元素和所有的参数
•push([ item1 [ , item2 [ , … ] ] ])方法:
–在数组尾添加一个或多个元素
•pop方法:
–从数组尾去除并返回元素
•shift方法:
–从数组头去除并返回元素
•unshift([ item1 [ , item2 [ , … ] ] ])方法:
–在数组头添加一个或多个元素
•join(separator)方法:
–返回以separator作为分割符得到一个连接所有元素的的字符串
–StringBuilder的基础,可以为IE加快字符串拼接速度
•reverse()方法:将数组内所有元素逆转
•sort(compareFunction)方法:
–参数为一个方法,用于比较两个元素
–省略了参数则直接使用<, ==, >比较两个元素

•slice(start, end)方法:
–返回新数组,不影响旧数组
–包含从下标start开始到下标end-1的元素
–如果省略end则包含从下标start开始至末尾的所有元素
–如果参数为负数,则表示数组的“倒数”第几个下标(即下标为n + array.length)
–如果start元素在end元素之后或相同,则返回空数组
–如果参数为负数,则表示数组的“倒数”第几个下标(即下标为n + array.length)
–如果start元素在end元素之后或相同,则返回空数组
•splice (start, deleteCount [ , item1 [ , item2 [ , … ] ] ] )方法:
–最灵活的方法,影响当前数组
–从下标start的元素开始,删除deleteCount个元素,并在当前start位置开始插入剩余元素
–删除元素:splice(2, 1)
–插入元素:splice(2, 0, "Hello", "World")
–替换元素:splice(2, 1, “Hello”, “World”)




html
    <div id="info"></div>
    
<script language="javascript" type="text/javascript">
        function display(text)
        {
            document.getElementById(
"info").innerHTML += (text + "<br />");
        }
        
        Array.prototype.display 
= function()
        {
            window.display(
"a: " + this);
        }
        
        var a 
= new Array(12345); // a = [1, 2, 3, 4, 5];
        display("Array initialized");
        a.display();
        
        display(
"a.push(6, 7)");
        a.push(
67); 
        a.display(); 
// [1, 2, 3, 4, 5, 6, 7]
        
        display(
"a.shift(): " + a.shift());
        a.display(); 
// [2, 3, 4, 5, 6, 7]
        
        display(
"a.unshift(8)");
        a.unshift(
8);
        a.display(); 
// [8, 2, 3, 4, 5, 6, 7];
        
        display(
"a.pop();" + a.pop());
        a.display(); 
// [8, 2, 3, 4, 5, 6];
        
        display(
"a.concat(9, 10): " + a.concat(910)); // [8, 2, 3, 4, 5, 6, 9, 10];
        a.display(); // [8, 2, 3, 4, 5, 6]
        
        display(
"'[' + a.join('][') + ']': " + '[' + a.join(']['+ ']'); // [8][2][3][4][5][6];
        
        display(
"a.slice(2, 5): " + a.slice(25)); // [3, 4, 5]
        display("a.slice(2, -2): " + a.slice(2-2)); // [3, 4]
        a.display();
        
        display(
"a.splice(3, 2)");
        a.splice(
32);
        a.display(); 
// [8, 2, 3, 6]
        
        display(
"a.splice(2, 0, 7, 8)");
        a.splice(
2078);
        a.display(); 
// [8, 2, 7, 8, 3, 6]
        
        display(
"a.splice(3, 1, 9, 10)");
        a.splice(
31910);
        a.display(); 
// [8, 2, 7, 9, 10, 3, 6]
        
        display(
"a.reverse()");
        a.reverse();
        a.display(); 
// [6, 3, 10, 9, 7, 2, 8]
        
        display(
"a.sort()");
        a.sort();
        a.display(); 
// [10, 2, 3, 6, 7, 8, 9]
        
        display(
"a.sort(function(x, y){ return y - x; });");
        a.sort(function(x, y){ 
return y - x; });
        a.display(); 
// [10, 9, 8, 7, 6, 3, 2];
    </script>
原文地址:https://www.cnblogs.com/timy/p/1181399.html