筛选数组元素方法

筛选原来数组中满足要求的元素,再将它赋值给新数组 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var arr=[3,45,63,23,56,22,11,65,25,67,84,26];
        var newarr=[];
        
        for (var i=0;i<arr.length;i++)
        {
            if(arr[i]>50)
            newarr[newarr.length]=arr[i];
            
        }
        alert(newarr);
    </script>
</body>
</html>

筛选数组元素方法

看看第二个方法有什么不足之处

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var arr=[3,45,63,23,56,22,11,65,25,67,84,26];
        var newarr=[];
        var j=0;
        for (var i=0;i<arr.length;i++)
        {
            if(arr[i]>50)
            newarr[j]=arr[i];
            j++;
        }
        alert(newarr);
    </script>
</body>
</html>

筛选数组元素方法

上一篇:快速逆平方根


下一篇:2021.7.23元素类型