zoukankan      html  css  js  c++  java
  • Jsの练习-数组其他常用方法 -map() ,filter() ,every() ,some()

    map() :映射,对数组中的每一项运行给定函数,返回每次函数调用结果组成的函数。

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    
    <body>
        <script>
            var arr = [1, 3, 5, 7, 9, 11];
            var newArr = arr.map(function (value, index) {
                return value * value;
            })
    
            console.log(newArr);
        </script>
    
    </body>
    
    </html>
    map()

    filter():过滤,对数组中的每一项运行给定函数,返回满足过滤条件组成的数组。

    filter()
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    
    <body>
        <script>
            var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
            var newArr = arr.filter(function (value, index) {
                return index % 3 === 0 || value >= 9;
            });
            console.log(newArr);
        </script>
    
    </body>
    
    </html>

    every(): 判断数组中每一项是否满足条件,只有所有项都满足条件,才会返回true。

    every()
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    
    <body>
        <script>
            var arr = [1, 2, 3, 4, 5];
    
            var newArr = arr.every(function (value, index) {
                return value < 10;
            })
    
            console.log(newArr);
        </script>
    
    </body>
    
    </html>

    some():判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。

  • 相关阅读:
    贝塞尔曲线
    那些有内容的文章——记录网址
    Mac 下 SVN 的使用
    让时间不再随系统设置而改变
    iOS 关闭图片渲染
    iOS的AssetsLibrary框架访问所有相片
    iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
    Linux 第一次学习笔记
    java第四次实验报告
    java第三次实验报告
  • 原文地址:https://www.cnblogs.com/-Tony/p/9220881.html
Copyright © 2011-2022 走看看