数组:
// map 映射,一个对一个
let arr=[2,33,5];
let result=arr.map(item=>item*2);
alert(result); //4;66;10
let score=[20,76,56,44,88,90];
let result2=score.map(item2=>item2>=60?'及格':'不及格');
alert(result2);
// reduce 汇总 一堆出来一个,如总数
let arr2 = [22,45,22,55,63,12,543,2324];
let result3=arr2.reduce(function(tep,items,index){
// alert(tep+','+items+','+index);
if(index=arr2.length-1){
return tep+items; //762,2324,7
}else{
return (tep+items)/arr2.length;
}
})
alert(result3); //3086
// filter 过滤器
let arr3=[23,45,676,454,33,4,66];
// var result4= arr3.filter(item3=>{
// if(item3%2==0){
// return true;
// }else{
// return false;
// }
// })
let result4= arr3.filter(item3=>item3%2==0); alert(result4);
let arr4=[
{title:'面包',price:40003},
{title:'火腿肠',price:23},
{title:'香蕉',price:4600},
{title:'例子',price:413}
]
let result5=arr4.filter(json=>json.price>=1000);
console.log(result5);
// forEach 迭代循环
let arr5=[3,2,5,36,7];
// arr5.forEach(item4=>{
// alert(item4);
// });
arr5.forEach((item4,index)=>{
alert(index+':'+item4);
});
网友评论