Blogger Information
Blog 25
fans 0
comment 0
visits 13651
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS中数组的不同类型及访问形式,流程中分支不同样式的演示实例
安超
Original
338 people have browsed it

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;
}

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:效果可以, 知识代码内容最好还是放在代码块中
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!