zoukankan      html  css  js  c++  java
  • 数组迭代的5个小宝贝(js)

    数组迭代的5个小宝贝(js)

    <script>
            // 1,数组.forEach(function (数组项, 下标, 数组本身) { });
            // 作用:同for循环,用于遍历数组(没有返回值)
            // var arr = ['刘备', '关羽', '张飞'];
            // arr.forEach(function (item, index, array) {
            //     console.log(item, index, array);
            // });
    
            // ---------------------------------
            // 2,数组.map(function (数组项, 下标, 数组本身) { });
            // 作用:循环数组,返回每次调用的结果组成的新数组
            // var arr = [3, 6, 4];
            // var n = arr.map(function (item, index) {
            //     // console.log(item, index);
            //     return item * 2;
            // });
            // console.log(n); // [ 6, 12, 8 ]
    
            // ------------------------------
            // 3,数组.filter(function (数组项, 下标, 数组本身) { });
            // 作用:返回每次调用的结果为true的项组成的数组
            // var arr = [4, 45, 24, 4, 2];
            // var n = arr.filter(function (item, index) {
            //     // console.log(item, index);
            //     return item > 10 && item < 30;
            // });
            // console.log(n);
    
            // ----------------------------
            // 4,数组.every(function (数组项, 下标, 数组本身) { });
            // 作用:每一项调用的结果为true,则返回true
            // var arr = [4, 45, 24, 4, 2];
            // var n = arr.every(function (item, index) {
            //     // console.log(item, index);
            //     return item > 0;
            // });
            // console.log(n);
    
            // -----------------------
            // 5,数组.some(function (数组项, 下标, 数组本身) { });
            // 作用:只要有一项返回true,结果就是true
            var arr = [4, 45, 24, 4, 2];
            var n = arr.some(function (item, index) {
                return item > 10;
            });
            console.log(n);
    
    
        </script>
  • 相关阅读:
    数据结构基础知识(2)
    ASIHttpRequest异步请求网络崩溃解决
    [置顶] Android仿人人客户端(v5.7.1)——采用ViewGroup做父容器,实现左侧滑动菜单(三)
    Linux中ifcfgeth0配置参数说明
    什么是redis
    CentOS网络接口配置文件ifcfgeth详解
    查看Linux版本
    CentOS7 network.service loaded failed 处理技巧
    如果这都不算爱
    ACL Security In Seam, Part 1
  • 原文地址:https://www.cnblogs.com/cyf666cool/p/13647346.html
Copyright © 2011-2022 走看看