JS-向数组指定位置添加元素

<!DOCTYPE html>
<html>
 
    <head>
        <meta charset="UTF-8">
        <title>array-api</title>
    </head>
 
    <body>
    </body>
    <script>
        var array = ["one", "two", "four"];
        console.log(JSON.stringify(array));//["one","two","four"]
        //在指定位置添加元素,第一个参数指定位置,第二个参数指定要删除的元素,如果为0,则追加
        array.splice(2, 0, "three");
        console.log(JSON.stringify(array));//["one","two","three","four"]
    </script>
 
</html>

 

posted @ 2018-12-03 14:34  expworld  阅读(36171)  评论(0编辑  收藏  举报