zoukankan      html  css  js  c++  java
  • 数组迭代

    var arr = [3,4,5,6,7,"a"];

    var isNum = function(elem,index,AAA){
    return !isNaN(elem);
    }

    var toUpperCase = function(elem){
    return String.prototype.toUpperCase.apply(elem);
    }

    var print = function(elem,index){
    console.log(index+"."+elem);
    }

    /*对数组中的每一项执行测试函数,直到获得对指定的函数返回 false 的项。 使用此方法 可确定数组中的所有项是否满足某一条件,类似于&&的含义*/
    var res = arr.every(isNum);
    console.log(res);//false;

    /*对数组中的每一项执行测试函数,直到获得返回 true 的项。 使用此方法确定数组中的所有项是否满足条件.类似于||的含义*/
    res = arr.some(isNum);
    console.log(res);//true

    /*对数组中的每一项执行测试函数,并构造一个新数组,返回 true的项被添加进新数组。 如果某项返回 false,则新数组中将不包含此项*/
    res = arr.filter(isNum);
    console.log(res);//[3, 4, 5, 6, 7]

    /*对数组中的每一项执行函数并构造一个新数组,并将原始数组中的每一项的函数结添加进新数组。*/
    res = arr.map(toUpperCase);
    console.log(res);//["3", "4", "5", "6", "7", "A"]

    /*对数组中的每一项执行函数,不返回值*/
    res = arr.forEach(print);
    console.log(res);

    //自己扩展

    /*Array.prototype.every = function(fun,obj) {
    var len = this.length;
    if (typeof fun != "function")
    throw new TypeError();
    for (var i = 0; i < len; i++) {
    if (!fun.call(obj,this[i], i,this))
    return false;
    }
    return true;
    };*/

  • 相关阅读:
    activemq 异步和同步接收
    简单的activemq的封装和使用
    activemq 一个不错的ppt
    activemq 的小实验
    activemq api的封装
    观察者设计模式例子
    【转载】自定义类加载器例子
    京东拍拍网 笔试 搞java的去考C++ 苦逼
    java中用equals比较两个内容相同的字符数组
    云端飘 String s=new String("abc")创建了几个对象?
  • 原文地址:https://www.cnblogs.com/guangyuan/p/7252599.html
Copyright © 2011-2022 走看看