JavaScript基础——对象方法

数组对象方法

1. push方法:在数组的最后添加元素

    let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    arr.push(11);
    console.log(arr);//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11]

2. pop方法:移除数组中的最后一个元素并返回该元素。(如果数组为空将返回unfefined)

	let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    arr.pop();
    console.log(arr); //[1, 2, 3, 4, 5, 6, 7, 8, 9]
    console.log(arr.pop());//9

3. unshift方法:在数组最前面添加元素

	let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
	arr.unshift(0);
	console.log(arr); //[0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	console.log(arr.unshift()); // 10 长度

4. shift方法:删除数组最前面的元素

	let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
	console.log(arr.shift()); //1 返回被删除的元素
	console.log(arr); //[2, 3, 4, 5, 6, 7, 8, 9, 10]

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); //[1, 2, 3, 4, 5, 6, 7, 5, 4, 3, 2, 1]

6.join方法:将数组中的每一个元素中间设置一个分隔符转换为字符串

	let strArr = ['元素1', '元素2', '元素3'];
	let str = strArr.join('-');
	console.log(str); //元素1-元素2-元素3

7.reverse方法:翻转数组

	let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
	arr.reverse(); //在原有的基础上翻转
	console.log(arr); //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

8.slice:截取数组方法:开始下标/结束下标(包头不包尾[0,3))

	let res = arr.slice(0, 3)
	console.log(res); // [1, 2, 3]

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); // [100, 200, 300, 1, 2, 3, 4, 5, 6, 7]

        // 删除元素
        arr2.splice(0, 3)
        console.log(arr2); //[2, 1]

        // 改变元素
        // 删除的同时添加
        arr.splice(0, 2, 100, 200);
        console.log(arr); //[100, 200, 3, 4, 5, 6, 7, 8, 9, 10]

        // 10.sort 方法:数组排序
        // let arr3 = [122, 22, 32, 4, 5, 1, 10, 100, 20];

10.sort 方法:数组排序

	let arr3 = [122, 22, 32, 4, 5, 1, 10, 100, 20];

        function fun(a, b) {
            // return a - b; //从小到大
            return b - a; //从大到小
        }
        arr3.sort(fun);
        console.log(arr3); //[122, 100, 32, 22, 20, 10, 5, 4, 1]

        // 拓展可以用排序的方法得到数组中的最大最小值
        function fun(a, b) {
            // return 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];
        // min = arr4.sort(fun);
        console.log(max); //122

字符串对象方法

1.concat 方法:拼接字符串

	let str1 = 'hello';
	let str2 = 'world';
	let res = str1.concat(str2);
	console.log(res); //helloworld

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);//HELLO
	let res2 = str2.toLocaleLowerCase();
	console.log(res2);//world

4.charAt方法:获得对应下标的字符

	let str = 'hello world';
	console.log(str.length); //长度 11 空格也算
	console.log(str[2]); //下标为2-->l
	console.log(str.charAt(2));// l

5.charCodeAt方法:获得对应下标的ascill码值

	let str = 'abcABC';
	let res = str.charCodeAt('a');
	console.log(res); //97

6.indexOf方法:判断某个字符是否存在

	let str = 'love';
	// let res = str.indexOf('t'); -1
	let res = str.indexOf('l'); 
	console.log(res);//0

7.split方法:把一个字符串变成数组

	let str = 'hello❤world❤!';
	let arr = str.split('❤'); //与❤作为分隔符,把字符串变成数组
	 console.log(arr); //["hello", "world", "!"]
	let str2 = arr.join('&');
     console.log(str2); //hello&world&!

时间对象方法

1.let time = new Date();:创建一个时间对象

	//创建时间对象
	let time = new Date();
	console.log(time); //Wed Jul 22 2020 16:08:23 GMT+0800 (中国标准时间)

2.time.toLocaleString()time.toLocaleDateString():获得日期

	//创建时间对象
	let time = new Date();
	console.log(time.toLocaleString()); //2020/7/22 下午4:08:23
	console.log(time.toLocaleDateString()); //2020/7/22

3.time.toTimeString()time.toLocaleTimeString():获得时间

	//创建时间对象
	let time = new Date();
	console.log(time.toTimeString()); //16:08:23 GMT+0800 (中国标准时间)
	console.log(time.toLocaleTimeString()); //下午4:08:23

4.time.getFullYear():获得年份

	//创建时间对象
	let time = new Date();
	console.log(time.getFullYear()); //2020

5.time.getMonth():获得月份(获得当前月需+1)

	//创建时间对象
	let time = new Date();
 	console.log(time.getMonth()); //从0开始计算,要获得当前月份需要+1

6.time.getDate():获得日期

	//创建时间对象
	let time = new Date();
	console.log(time.getDate()); //获得当前日期

7.time.getDay():获得星期几

	//创建时间对象
	let time = new Date();
 	console.log(time.getDay());//3

8.Date.now():获得一个当前的秒(特殊)

	let sec = Date.now(); //从1970年1月一号开始计算
	console.log(sec);
	let newDate = new Date(1595320404134) //获得当前时间
	console.log(newDate); //Tue Jul 21 2020 16:33:24 GMT+0800 (中国标准时间)
  • 字符串案例练习
<script>
        // 有以下字符串:'name:果果-age:38-gender:男';
        // 使用js代码,将以上字符串变成一个对象
        /* 
            {
                name:'果果',
                age:38,
                gender:'男';
            }
         */
        let str = 'name:果果-age:38-gender:男';
        let arr = str.split('-');
        console.log(arr);
        /* 
            数组中的第一个元素
            let arr0 = arr[0].split(':');
            console.log(arr0);

            利用key 值来赋值内容
            per[arr0[0]]=arr0[1];
            console.log(obj);
        */

        let per = {};
        for (let i = 0; i < arr.length; i++) {
            let resArr = arr[i].split(':');
            per[resArr[0]] = resArr[1];
        }
        console.log(per);
    </script>

猜你喜欢

转载自blog.csdn.net/weixin_44757417/article/details/107497347