1.JS中数组的不用样式主要有
a.单维数组;
let arr =[“one”,”two”,”three”];
let arr_1 =arr[0];
console.log(arr_1);
// 可以使用foreach循环获取项目中的所有项
arr.forEach(item=>console.log(“this is the:”+item))
// forEach的两个参数
console.log(“forEach具备两个参数”);
arr.forEach((item,key)=>console.log(this is the ${key} number:${item}
))
b.多维数组,单维数组中,如果某一项也为数组,那么整体数组即为单维数组
let arrMuliti=[
[“one”,”two”,”three”],
[“first”,”second”,”third”]
]
c. 获得数组中的某一项:
let arrMuliti_1 = arrMuliti[0][0]
console.log(arrMuliti_1);
// 可以使用foreach循环获取项目中的所有项
console.log(“以下为循环显示多维数组的各项内容”);
arrMuliti.forEach(item=>item.forEach(subitem=>console.log(subitem)));
arrMuliti.forEach((item,key)=>item.forEach((subitem,subkey)=>console.log( This is the outside ${key} inside ${subkey} : ${subitem}
)));
d.对象数组
console.log(“\n”);
let objectArr = [
{one:1,two:2,three:3},
{first:”1st”,sencond:”2nd”,third:”3rd”},
{chengdu:”成都”,beijing:”北京”,hefei:”合肥”}
];
console.log(objectArr[0].one);
e.循环遍历对象数组
console.log(‘——————————‘);
console.log(“遍历对象数组:”);
objectArr.forEach( (item , key )=>console.log(This is the ${key} object:
)+” “ + Object.values(item).forEach(items=> console.log( items)))
let obj_2 = {one:”one1”,two:”two2”,three:”three3”};
// 对象的第一种遍历
for(let items in obj_2){
console.log(obj_2[items]);
}
console.log(‘————————-‘);
// 对象的第二种遍历
for(let value of Object.values(obj_2)){
console.log(value);
}
console.log(‘—————————————-‘);
2.分支判断的不同类型
// 分支判断主要有
// 1.单分支if
// 2. 双分支 if else
// 3.多分支
// 3.1:if/else if/else
// 3.2.switch/case
// 下面为if else if else 分支,注意 else if是分开写的
let age = 61;
a.单分支
if(age === 60){
console.log(“your age is 60”);
}
console.log(‘——————————————————‘);
b.双分支
if(age ===60){
console.log(‘your age is 60’);
}
else{
console.log(“your age is others”);
}
console.log(“———————————————————“);
双分支简化
console.log(“双分支简化结构”);
age === 60?console.log(“your age is 60”):console.log(“your age is others”);
console.log(“——————————————————-“);
c.多分支if else if else
if(age < 0 ){
console.log(“your age isn’t right”);
}else if(age < 55){
console.log(“your age is less than 55”);
}else if( age > 70){
console.log(“your age is greater than 70”);
}
else{
console.log(“your age is between 55 and 70”);
}
console.log(‘————————————————-‘);
// switch/case分支
let name = “jiao”;
switch(name){
case “one”:
console.log(“your input is one”);
break;
case “jiao”:
console.log(“your input is right”);
break;
case “two”:
console.log(“your input is two”);
break;
default:
console.log(“please input”);
break;
}