Blogger Information
Blog 49
fans 1
comment 0
visits 44869
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
用foreach遍历数组,查看手册了解splice()的使用(使用foreach遍历数组,在console中显示;利用splice增加和删除数组中的元素)2019年5月7日20点
Nick的博客
Original
782 people have browsed it

用foreach遍历数组,在console中显示;利用splice增加和删除数组中的元素

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>5.7作业</title>
</head>
<body>
<script>
    // 用foreach遍历数组
    var arr = ['html','css','javascript','php'];//先定义数组
    arr.forEach(function (value,key,array) {
        console.log('[' + key + '] ==>' + value);
        // console.log('[' + key + '] ==>' + array[key])
    });


    // 用splice()增加元素
    // 利用上面arr的元素来进行测试
    document.write(arr + '<br>');//现在网页中输出原数组内的元素
    arr.splice(-1,0,'thinkphp');//在数组倒数第二的位置上增加一个元素
    document.write(arr + '<br>');//输出最新的数组元素

    // 用splice()删除元素
    arr.splice(0,1);//在数组中删除第一位置上的元素,并且不再其位置上添加新元素
    document.write(arr + '<br>');//输出最新的数组元素


    arr.splice(2,1,'html');//将数组中第三位置的元素删除并替换为'html'
    document.write(arr);//输出最新的数组元素
</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


输出显示:作业显示效果.png

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!