字符串的常用方法
let str = "957461674@qq.com";
// concat():字符串拼接
let str1 = "我的邮箱:".concat(str);
console.log(str1); // 我的邮箱:957461674@qq.com
// slice(start,end):截取字符串,从start开始截取到end,不包含end,第二个参数如果不写则默认截取到最后一个
let str2 = str.slice(2, 5);
console.log(str2); // 746
// substr(start,end) 第二个参数指定获取字符的数量
let str3 = str.substr(2, 6);
console.log(str3); // 46167
// trim() 删除两边的空白字符
let str4 = " 哈哈哈哈哈 ";
console.log(str4.trim()); // 哈哈哈哈哈
// 字符串转数组
let res = "html JavaScript php vue";
let res1 = res.split(" "); // ["html", "JavaScript", "php", "vue"]
console.log(res1);
数组的常用方法
et arr = [];
// push:在尾部添加元素
arr.push(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
console.log(arr); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
// pop:在尾部删除元素
arr.pop();
console.log(arr); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
// unshift:在头部增加元素
arr.unshift(0);
console.log(arr); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
// shift:在头部删除元素
arr.shift();
console.log(arr); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
// join:数组转字符串
let a = arr.join("</li><li>");
a = `<ul><li>${a}</li>></ul>`;
console.log(a);
document.write(a);
// 拼接数组:concat()
console.log(arr.concat(["php"])); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, "php"]
// splice():取子元素
console.log(arr.splice(0, 4)); // [1, 2, 3, 4]
// splice数显数组的增删改操作
// 1、新增
console.log(arr);
let q = arr.splice(2, 0, "哈哈");
console.log(arr); // [5, 6, "哈哈", 7, 8, 9, 10, 11]
// 2、新增替换
let w = arr.splice(2, 2, "嘿嘿", "吼吼");
console.log(arr); // [5, 6, "哈哈", 7, 8, 9, 10, 11]
// forEach()
arr.forEach((item, i) => {
console.log(item + "---" + i);
});
// filter()
console.log(arr.filter((item) => item % 2 == 0)); // arr.filter((item)=> item % 2 == 0);
s = [1, 23, 4, 5, 6];
s1 = s.reduce((prev, next) => {
return prev + next;
});
console.log(s1); // 39
循环
// while()
let color = ["red", "blue", "green"];
let i = 0;
while (i < color.length) {
console.log(color[i]);
i++;
}
console.log("********************************");
// do while()
i = 0;
do {
console.log(color[i]);
i++;
} while (i < color.length);
// for in 循环对象
let obj = {
name: "yajie",
age: "18",
emal: "vueadmin@qq.com",
};
for (let j in obj) {
console.log("k = %o,v = %o", j, obj[j]);
}
/*
k = "name",v = "yajie"
k = "age",v = "18"
k = "emal",v = "vueadmin@qq.com"
*/
// for of
let w = [1,5,6,98,9];
for(let e of w){
console.log(e);
}
分支结构
let score = 80;
if (score >= 90 && score <= 100) {
alert("兄弟,牛批啊考的不错!");
} else if (score >= 80 && score < 90) {
alert("good!good!还不错!");
} else if (score >= 60 && score < 80) {
alert("啧啧,要加把劲咯!");
} else if (score > 100 && score < 0) {
alert("请输入0-100之间的数值");
} else {
alert("儒了子,考的什么玩意!");
}
switch (true) {
case score >= 90 && score <= 100:
alert("兄弟,牛批啊考的不错!");
break;
case score >= 80 && score < 90:
alert("good!good!还不错!");
break;
case score >= 60 && score < 80:
alert("啧啧,要加把劲咯!!");
break;
case score > 100 && score < 0:
alert("请输入0-100之间的数值");
break;
default:
alert("儒了子,考的什么玩意!");
break;
}