数组方法
//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(", "));
网友评论