Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
if(condition){
...
}
if(condition){
...
}else{
...
}
(condition) ? ... : ... ;
if(condition){
...
}else if(condition2){
...
}else{
...
}
//区间判断传true , 单值判断传变量
switch(true){
case condition:
...
break;
case condition2:
case condition3:
//case可以多个
...
break;
default:
...
}
let array = [1,2,3];
i = 0;
while (i < array.length) {
console.log(array[i]);
//更新条件
i++;
}
i = 0;
do {
console.log(array[i]);
//更新条件
i++;
}while(i < array.length)
for (let i=0;i<array.length;i++){
console.log(array[i]);
}
for(let item of array.entries){
console.log(item);
}
for (let key in obj){
console.log(obj[key]);
}
// item循环中数据/index键/数据
obj.map((item,index,array)=>{
return item;
});
//案例
let a = [
{"name":"1号",num:1,param:[{'color':"red"}]},
{"name":"2号",num:2,param:[{'color':"blue"}]},
{"name":"3号",num:3,param:[{'color':""}]},
{"name":"4号",num:4},
{"name":"5号",num:5},
];
a.map((item)=>{
item['color'] = item['param'] && item['param'][0]['color']?item['param'][0]['color']:'';
return item;
});
// prev上次return的数据/item循环中数据/index键/数据
obj.reduce((prev,item,index,array)=>{
return item;
});
//案例
var arr = [1, 2, 3, 4];
var sum = arr.reduce(function(prev, cur, index, arr) {
console.log(prev, cur, index);
return prev + cur;
})
console.log(arr, sum);
const f = (a,b=0)=>{
return a + b;
}
const f = (a,...b)=>{
return b;//把剩余参数放入数组中
}
console(...[1,2,3,4]);
// 1 2 3 4
let f = (a,b,...arr)=>{
return arr;
}
let name = 'name';
const user = {
name,//name:name(原)
getName(){
return this.name;
},
//原
//getName:function(){
// return this.name
//}
}