JavaScript filter array

Claim:

From an array, filter out the elements that meet the conditions and put them in a new array.
There is an array [1, 19, 2, 8, 9, 15, 11, 7, 6, 4, 18, 10], delete more than 10 elements.

Code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var arr = [1, 19, 2, 8, 9, 15, 11, 7, 6, 4, 18, 10];
        var newArr = [];
        for (var i = 0; i < arr.length; i++) {
     
     
            if (arr[i] <= 10) {
     
     
                // newArr[newArr.length] = arr[i];
                newArr.push(arr[i]);
            }
        }
        console.log(newArr);
    </script>
</body>

</html>

Output result:

Insert picture description here

Guess you like

Origin blog.csdn.net/Jack_lzx/article/details/109242130