JavaScript中的Array对象的方法 下

<script type="text/javascript">
    /*
    //concat 方法 (Array)
    //返回一个新数组,这个新数组是由两个或更多数组组合而成的。
    var a = [1, 2, 3, 4, 5];
    var b = "北京";
    var c = ["足球", "篮球"];
    var d = a.concat(b, c);
    window.document.write(d); //输出  1,2,3,4,5,北京,足球,篮球
    */

    /*
    //join 方法
    //返回字符串值,其中包含了连接到一起的数组的所有元素,元素由指定的分隔符分隔开来。
    var a = [1, 2, 3, 4, 5];
    var b = a.join("-"); 
    window.document.write(b);//输出  1-2-3-4-5
    */

    /*
    //pop 方法
    //移除数组中的最后一个元素并返回该元素。
    var a = [1, 2, 3, 4, 5];
    var b = a.pop();
    window.document.write(a); //输出  1,2,3,4
    window.document.write("------------");
    window.document.write(b); //输出 5
    */


    /*
    //push 方法
    //将新元素添加到一个数组中,并返回数组的新长度值。
    var a = [1, 2, 3, 4, 5];
    var b = a.push(11);
    window.document.write(a); //输出  1,2,3,4,5,11
    window.document.write("------------");
    window.document.write(b); //输出 6
    */

    /*
    //reverse 方法
    //返回一个元素顺序被反转的 Array 对象。
    var a = [1, 2, 3, 4, 5];
    var b = a.reverse();
    window.document.write(a); //输出  5,4,3,2,1
    window.document.write("------------");
    window.document.write(b); //输出 5,4,3,2,1
    */

    /*
    //shift 方法
    //移除数组中的第一个元素并返回该元素。
    var a = [1, 2, 3, 4, 5];
    var b = a.shift();
    window.document.write(a); //输出  2,3,4,5
    window.document.write("------------");
    window.document.write(b); //输出 1
    */

    /*
    //slice 方法 (Array)
    //返回一个数组的一段。
    //arrayObj.slice(start, [end]) 
    var a = [1, 2, 3, 4, 5];
    var b = a.slice(1, 3);
    window.document.write(a); //输出  1,2,3,4,5
    window.document.write("------------");
    window.document.write(b); //输出 2,3
    */

    /*
    //sort 方法
    //返回一个元素已经进行了排序的 Array 对象。 
    var a = [1, 2, 3, 4, 5];
    var b = a.sort(function () { return 2; });
    window.document.write(a); //输出  5,4,3,2,1
    window.document.write("------------");
    window.document.write(b); //输出 5,4,3,2,1
    */

    /*
    //splice 方法
    //从一个数组中移除一个或多个元素,如果必要,在所移除元素的位置上插入新元素,返回所移除的元素。

    var a = [1, 2, 3, 4, 5];
    var b = a.splice(2, 2, [22, 33]);
    window.document.write(a); //输出  1,2,22,33,5
    window.document.write("------------");
    window.document.write(b); //输出 3,4
    */

    /*
    //toString 方法
    //返回对象的字符串表示。 
    var a = [1, 2, 3, 4, 5];
    var b = a.toString();
    window.document.write(a); //输出  1,2,3,4,5
    window.document.write("------------");
    window.document.write(b); //输出 1,2,3,4,5
    */

    /*
    //unshift 方法
    //将指定的元素插入数组开始位置并返回该数组。
    
    var a = [1, 2, 3, 4, 5];
    var b = a.unshift([22, 33, 44]);

    window.document.write(a); //输出  22,33,44,1,2,3,4,5
    window.document.write("------------");
    window.document.write(b); //输出 6
    */
</script>
原文地址:https://www.cnblogs.com/haowuji/p/3056224.html