美文网首页
数组方法

数组方法

作者: 郝特么冷 | 来源:发表于2017-09-22 10:48 被阅读2次

    数组方法

    //isArray方法
        var months = ["January","Febraury","March","April","May","june","July","August","September","October","November","December"],
        items = {
            "0":"January",
            "1":"February",
            "2":"March",
            "3":"April",
            "4":"May"
        };
        console.log(Array.isArray(months)); //true
        console.log(Array.isArray(items)); //false
        
        
        //forEach方法
        months.forEach(function(value,index,fullArray){
            console.log(value + "is month number" + (index + 1) + "of" + fullArray.length);
        })
        //every和some方法
        var everyItemContainsR  = months.every(function(value,index,fullArray){
            return value.indexOf("r") >= 0;
        })
        //只有每一个都包含r才会返回true
        console.log(everyItemContainsR);//false
        var someItemContainsR = months.some(function(value,index,fullArray){
            return value.indexOf("r") >= 0;
        })
        //有一部分包含r就会返回true
        console.log(someItemContainsR);//true
        
        
        //map方法
        var daysOfTheWeek = ["Monday","Tuesday","Wednesday"],
        daysFirstLetters = daysOfTheWeek.map(function(value,index,fullArray){return value+"starts with"+value.charAt(0)});
        console.log(daysFirstLetters.join(", "));
        
        //filter方法
        monthsContainingR = months.filter(function(value,index,fullArray){
            return value.indexOf("r") >= 0;
        })
        console.log(monthsContainingR.join(", "));
    

    相关文章

      网友评论

          本文标题:数组方法

          本文链接:https://www.haomeiwen.com/subject/imnxextx.html