JavaScript的数组常用方法

数组常用方法

1. push

=> 语法 arr.push(数据1, 数据2, 数据3, ...)
=> 作用 把所有的参数按照顺序追加到数组的末尾
=> 返回值,追加以后数组的长度
=> 直接操作原始数组

2. pop

=> 语法 数组.pop()
=> 作用 删除数组的最后一个数据
=> 返回值 被删除的数据
=> 直接操作原始数组

3. unshift()

=> 语法 数组.unshift(数据1, 数据2, 数据3, ...)
=> 作用 从数组最前面插入一些数据
=> 返回值 插入后的数组长度
=> 直接操作原始数组

4. shift()

=> 语法 数组.unshift()
=> 作用 删除数组的最前面一个元素
=> 返回值 被删除的数据
=> 直接操作原始数组

5. reverse()

=> 语法 数组.reverse()
=> 作用 反转数组
=> 返回值 反转后的数组
=> 直接操作原始数组

6. sort()

=> 语法 数组.sort()
  1. arr.sort()
    -> 按照每一个数据中的每一位数组的ASCII码进行排列
  2. arr.sort(function (a, b) {return a -b})
    -> 从小到大排
  2. arr.sort(function (a, b) {return b -a})
    -> 从大到小排
=> 作用 数组排序
=> 返回值 排序后的数组
=> 直接操作原始数组

7. splice()

=> 语法 数组.splice(开始索引,多少个)
  1. 数组.splice(开始索引,多少个)
    -> 从开始索引,截取多少个
    -> 第二个参数可以不写,直接到末尾
  2. 数组.splice(开始索引,多少个,替换数组1,替换数组2,替换数组3, ...)
    -> 把替换数据按照顺序插入到你街区的位置
    -> 注意 从那个位置开始索引删除,就替换数据的第一个就插入到哪个位置
=> 作用 
  1. 截取数组
  2. 替换新内容
=> 返回值 一定是一个数组
=> 如果你截取一个数据,数组里面有一个
=> 如果你一个都不截取,那么是一个空数组
=> 直接操作原始数组

8. concat()

=> 语法 数组.concat(数据1,数据2)
=> 作用 
    -> 如果参数是数组,那么把数组拆开,里面每一项追加到原数组后面
    -> 如果是参数数据,那么直接追加
=> 返回值 追加好的数组
=> 不改变原始数组

9. slice()

=> 语法
    1. 数组.slice(开始索引, 结束索引) [)
        第一个参数可以不写,表示从头
        第二个参数可以不写,表示到尾
    2. 数组.slice(开始索引, 结束索引) [) 
        -> 参数可以写一个负整数
        -> length - 负数
=> 作用 获取数组里的某些数据
=> 返回值 一个数组
    -> 如果你获取多个数据,数组里面有多个
    -> 如果你获取一个数据,数组里面有一个
    -> 如果你一个都不获取,那么是个空数组
=> 不改变原始数组

10. join()

=> 语法:数组.join("连接符号")
    -> 不传递参数是按照(,)连接
    -> 你传递什么按照什么连接
=> 作用:把数字里面得每一个数据使用连接符号连接在一起
=> 返回值: 是一个连接好的内容,是一个String 类型

11. indexOf()

=> 语法:
    -> 数组.indexOf(数据)
    -> 数组.indexOf(数据, 开始索引) 从哪个索引开始查找
=> 作用: 正向查找数组里面指定这个数据的索引
=> 返回值: 
    -> 如果有这个数据,是第一个满足条件的数据的索引
    -> 如果没有这个数据,那么是 -1

12. lastIndexOf()

=> 语法:
    -> 数组.lastIndexOf(数据)
    -> 数组.lastIndexOf(数据, 开始索引) 从哪个索引开始往前查找
=> 作用: 反向查找数组里面指定这个数据的索引
=> 返回值: 
    -> 如果有这个数据,是第一个满足条件的数据的索引
    -> 如果没有这个数据,那么是 -1  
    -> 虽然是从后往前索引,但是索引还是正常索引

13. forEach()

=> 语法:数组.forEach(function (item, index, arr){})
    -> item 数组的每一项
    -> index 数组的索引
    -> arr 原始数组
=> 作用:取代for循环的作用,遍历数组
=> 没有返回值

13. map()

=> 语法:数组.map(function (item, index, arr){})
    -> item 数组的每一项
    -> index 数组的索引
    -> arr 原始数组
=> 作用:映射数组
=> 返回值: 
    -> 新的数组
    -> 里面是对原始数组每一个数据的操作
    -> 返回值数组,一定和原始数组长度一样
=> 不改变原始数组

对应数组操作代码

		// 1.push
        var arr = ["hello", "world", "你好", "世界"]
        var res = arr.push("新来的", "新来的2", "新来的3")
        console.log(res);
        console.log(arr);

        //  2.pop
        var arr = ["hello", "world", "你好", "世界"]
        var res = arr.pop();
        console.log(res);
        console.log(arr);

        // 3. unshift
        var arr = ["hello", "world", "你好", "世界"]
        var res = arr.unshift("新来的1", "新来的2");
        console.log(res);
        console.log(arr);

        // 4. shift
        var arr = ["hello", "world", "你好", "世界"]
        var res = arr.shift("新来的1", "新来的2");
        console.log(res);
        console.log(arr);
         // 5. reverse()
    	var arr = ["hello", "world", "你好", "世界"]
    	var res = arr.reverse();
    	console.log(res);
    	console.log(arr);

    	// 6. sort()
    	var arr = [1, 11, 133, 26, 51, 19, 32, 27, 15]
    	var res = arr.sort();
    	console.log(res);
    	console.log(arr);

	    var res = arr.sort(function(a, b) {
       	 	return a - b
	    })
    	console.log(res);
    	console.log(arr);
    	var res = arr.sort(function(a, b) {
        	return b - a
    	})
    	console.log(res);
	    console.log(arr);


	    // 7. splice()
	    var arr = [1, 11, 133, 26, 51, 19, 32, 27, 15]
        // var res = arr.splice(1, 3);
        // console.log(res);
        // console.log(arr);

	    var res = arr.splice(1, 0, "新来的", "新来的二");
	    console.log(res);
	    console.log(arr);


	    // 8. concat()
	    var arr = [1, 11, 133, 26, 51, 19, 32, 27, 15]
	    var res = arr.concat([10, 20], [30, 40], 100);
	    console.log(res);
	    console.log(arr);
	
	
	    // 9. slice
	    var arr = [1, 11, 133, 26, 51, 19, 32, 27, 15]
	    var res = arr.slice(1, 3);
	    console.log(res);
	    console.log(arr);
	
	    var res2 = arr.slice(-8, -6);
	    console.log(res2);
	    console.log(arr);
	
	
	    // 10. join()
	    var arr = [1, 11, 133, 26, 51, 19, 32, 27, 15]
	    var res = arr.join("@-@");
	    var res = arr.join();
	    console.log(res);
	    console.log(arr);
	
	
	    // 11. indexOf()
	    var arr = [1, 11, 133, 26, 51, 19, 32, 11, 15]
	    var res = arr.indexOf(11);
	    console.log(res);
	    console.log(arr);
	
	    var res = arr.indexOf(11, 4);
	    console.log(res);
	    console.log(arr);
	
	
	    // 12. LastIndexOf()
	    var arr = [1, 11, 133, 26, 51, 19, 32, 11, 15]
	    var res = arr.lastIndexOf(11);
	    console.log(res);
	    console.log(arr);
	
	    var res = arr.lastIndexOf(11, 5);
	    console.log(res);
	    console.log(arr);
	
	
	    // 13. forEach()
	    var arr = [1, 11, 133, 26, 51, 19, 32, 11, 15]
	    let res = arr.forEach(function(item, index, arr) {
	        // 这个函数,会根据数组里面有多少个数据执行多少回
	        // console.log("我执行了")
	
	        // item 分别是这个数组里面的每一项
	        console.log(item);
	        console.log(index);
	        console.log(arr);
	    })
	
	
	    // 14. map()
	    var arr = [1, 11, 133, 26, 51, 19, 32, 11, 15]
	    var res = arr.map(function(item, index, arr) {
	        console.log(item, "-----", index, "-----", arr);
	        return item * 1.3;
	    })
	    console.log(res);
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值