数组对象方法
1. push方法
:在数组的最后添加元素
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
arr.push(11);
console.log(arr);
2. pop方法
:移除数组中的最后一个元素并返回该元素。(如果数组为空将返回unfefined)
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
arr.pop();
console.log(arr);
console.log(arr.pop());
3. unshift方法
:在数组最前面添加元素
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
arr.unshift(0);
console.log(arr);
console.log(arr.unshift());
4. shift方法
:删除数组最前面的元素
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(arr.shift());
console.log(arr);
5. concat方法
:拼接数组
let arr1 = [1, 2, 3, 4, 5, 6, 7];
let arr2 = [5, 4, 3, 2, 1];
let newArr = arr1.concat(arr2);
console.log(newArr);
6.join方法
:将数组中的每一个元素中间设置一个分隔符转换为字符串
let strArr = ['元素1', '元素2', '元素3'];
let str = strArr.join('-');
console.log(str);
7.reverse方法
:翻转数组
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
arr.reverse();
console.log(arr);
8.slice
:截取数组方法:开始下标/结束下标(包头不包尾[0,3)
)
let res = arr.slice(0, 3)
console.log(res);
9.splice方法
:增删改一体
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let arr1 = [1, 2, 3, 4, 5, 6, 7];
let arr2 = [5, 4, 3, 2, 1];
splice(index,howmany,item1,item2...)
arr1.splice(0, 0, 100, 200, 300);
console.log(arr1);
arr2.splice(0, 3)
console.log(arr2);
arr.splice(0, 2, 100, 200);
console.log(arr);
10.sort 方法
:数组排序
let arr3 = [122, 22, 32, 4, 5, 1, 10, 100, 20];
function fun(a, b) {
return b - a;
}
arr3.sort(fun);
console.log(arr3);
function fun(a, b) {
return b - a;
}
let arr4 = [122, 22, 32, 4, 5, 1, 10, 100, 20];
let max;
let min;
max = arr4.sort(fun)[0];
console.log(max);
字符串对象方法
1.concat 方法
:拼接字符串
let str1 = 'hello';
let str2 = 'world';
let res = str1.concat(str2);
console.log(res);
2.replace方法
:替换字符
let str = '把我替换成他';
let res = str.replace('我', '他')
console.log(res);
console.log(str);
3.toLocaleUpperCase()
转换为大写toLocaleLowerCase()
转换为小写
let str1 = 'hello';
let str2 = 'WORLD';
let res1 = str1.toLocaleUpperCase();
console.log(res1);
let res2 = str2.toLocaleLowerCase();
console.log(res2);
4.charAt方法
:获得对应下标的字符
let str = 'hello world';
console.log(str.length);
console.log(str[2]);
console.log(str.charAt(2));
5.charCodeAt方法
:获得对应下标的ascill码值
let str = 'abcABC';
let res = str.charCodeAt('a');
console.log(res);
6.indexOf方法
:判断某个字符是否存在
let str = 'love';
let res = str.indexOf('l');
console.log(res);
7.split方法
:把一个字符串变成数组
let str = 'hello❤world❤!';
let arr = str.split('❤');
console.log(arr);
let str2 = arr.join('&');
console.log(str2);
时间对象方法
1.let time = new Date();
:创建一个时间对象
let time = new Date();
console.log(time);
2.time.toLocaleString()
、time.toLocaleDateString()
:获得日期
let time = new Date();
console.log(time.toLocaleString());
console.log(time.toLocaleDateString());
3.time.toTimeString()
、time.toLocaleTimeString()
:获得时间
let time = new Date();
console.log(time.toTimeString());
console.log(time.toLocaleTimeString());
4.time.getFullYear()
:获得年份
let time = new Date();
console.log(time.getFullYear());
5.time.getMonth()
:获得月份(获得当前月需+1)
let time = new Date();
console.log(time.getMonth());
6.time.getDate()
:获得日期
let time = new Date();
console.log(time.getDate());
7.time.getDay()
:获得星期几
let time = new Date();
console.log(time.getDay());
8.Date.now()
:获得一个当前的秒(特殊)
let sec = Date.now();
console.log(sec);
let newDate = new Date(1595320404134)
console.log(newDate);
<script>
let str = 'name:果果-age:38-gender:男';
let arr = str.split('-');
console.log(arr);
let per = {};
for (let i = 0; i < arr.length; i++) {
let resArr = arr[i].split(':');
per[resArr[0]] = resArr[1];
}
console.log(per);
</script>