处理集合_删除数组元素的粗略方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>删除数组元素的粗略方法</title>
    <script src="../unitl/test.js"></script>
    <style>
        #results li.pass {color:green;}
        #results li.fail {color:red;}
    </style>


</head>

<body>
    <ul id="results"></ul>

</body>

<script>

    const ninjas = ["Yagyu","Kuma","Hattori","Fuma"];

    delete ninjas[1];


    //虽然删除了元素的值,但是数组长度仍然为4。
    assert(ninjas.length === 4,"Length still reports that there are 4 item");
    assert(ninjas[0]==="Yagyu","First item is Yagyu");
    assert(ninjas[1] === undefined,"We've simply created a hole");
    assert(ninjas[2] === "Hattori","Hattori is still the third item");
    assert(ninjas[3] === "Fuma","And Fuma is the last item");



</script>
</html>

这种删除数组元素无效,只是在数组中创建了一个空元素。数组仍然有4个元素,其中我们想要删除的元素是undefined。
原文地址:https://www.cnblogs.com/jamal/p/14060409.html