js数组的常用方法

js数组的常用方法(不包含ES6中新增的方法)

一、 改变原数组的方法

1. push() 后增
向数组的末尾添加新的元素,返回数组的长度
        var a = [2,3,9,6,22];
        var b = a.push(11);
        console.log(b);  // 6
        console.log(a);  // [1,3,9,6,22,11]
2. pop() 剪切数组的最后一位元素
返回的是数组最后一位元素
        var a = [2,3,9,6,22];
        var b = a.pop();
        console.log(b);  // 22
        console.log(a);  // [2,3,9,6]
3. shift() 前删
删除数组最前部的元素,返回该元素
        var a = [2,3,9,6,22];
        var b = a.shift();
        console.log(b);  // 2
        console.log(a);  // [3,9,6,22]
4. unshift() 前增
从头部开始添加元素,返回添加后的数组长度
        var a = [2,3,9,6,22];
        var b = a.unshift(15);
        console.log(b);  // 6
        console.log(a);  // [15,2,3,9,6,22]
5. reverse() 逆转,反转
颠倒数组内元素的顺序
        var a = [2,3,9,6,22];
        var b = a.reverse();
        console.log(b);  // [22, 6, 9, 3, 2]
        console.log(a);  // [22, 6, 9, 3, 2]
6. splice() 修改、删除和添加
splice(startIndex,length,增加的元素1,增加的元素2,…,增加的元素n) , 从startIndex开始删除长度为length的元素,并从删除的位置开始添加新的元素1-n,返回的是所删除元素的数组
        var a = [2,3,9,6,22];
        var b = a.splice(1,2,18,41);
        console.log(b);  // [3,9]
        console.log(a);  // [2,18,41,6,22]
7. sort() 排序
根据ascⅡ值来进行的排序,默认升序,返回排序后的数组
        var a = [2,3,9,6,22];
        var b = a.sort();
        console.log(b);  // [2, 22, 3, 6, 9]
        console.log(a);  // [2, 22, 3, 6, 9]

但是可以通过回调函数来控制按值排序

        a.sort(function(a, b){
           return a - b;  // 升序  
           // b - a为降序
        });
        console.log(a);  // [2,3,6,9,22]

二、不改变原数组的方法

1. concat() 进行数组的拼接,返回拼接后的数组
        var a = [1,2,3,4];
        var b = [5,6,7];
        var c = a.concat(b);
        console.log(c);  // [1,2,3,4,5,6,7]
        console.log(a);  // [1,2,3,4]
2. slice() 剪切
slice(startIndex, endIndex) 从startIndex剪切到endIndex,其中剪切的部分不包括endIndex这一位,返回剪切的元素组成的数组
        var a = [1,2,3,4,5];
        var b = a.slice(1,3);
        console.log(b);  // [2,3]
        console.log(a);  // [1,2,3,4,5]
3. join() 拼接
将数组拼接成字符串,返回的是拼接好后的字符串
        var a = [1,2,3,4,5];
        var b = a.join('--');
        console.log(b);  // '1--2--3--4--5'
        console.log(a);  // [1,2,3,4,5]
4. toString() 将数组转化成字符串
返回的是转化好的字符串
        var a = [1,2,3,4];
        var b = a.toString();
        console.log(b);  // '1,2,3,4'
        console.log(a);  // [1,2,3,4]
5. filter() 过滤
返回数组中满足条件的元素组成的新数组
        var a = [1,2,3,4];
        var b = a.filter(function (t, number, ts) {
           return t < 3;
        });
        console.log(b);  // [1,2]
        console.log(a);  // [1,2,3,4]
6. map() 格式化
根据需求格式化数组,返回格式化后的数组
        var a = [1,2,3,4];
        var b = a.map(function (t, number, ts) {
           return t + 1;
        });
        console.log(b);  // [2,3,4,5]
        console.log(a);  // [1,2,3,4]
7. indexOf()和lastIndexOf()
indexOf(某元素,startIndex) 从startIndex开始查找某元素在数组中的位置,若存在则返回第一个该元素的下标;否则,返回-1。lastIndexOf()和indexOf()一样,只不过是从尾部向首部查询。
        var a = [3,44,17,28];
        var b = a.indexOf(44);
        console.log(b);  // 1
        console.log(a);  // [3,44,17,28]
8.forEach 遍历
        var a = [3,44,17,28];
        a.forEach(function (t, number, ts) {
            console.log(number, t);
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值