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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM