數組:
// 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 迭代循環(huán)
let arr5=[3,2,5,36,7];
// arr5.forEach(item4=>{
// alert(item4);
// });
arr5.forEach((item4,index)=>{
alert(index+':'+item4);
});