数组常用方法举例

1.    字符串以逗号分隔成数组的方法   : 

                                                var list = code.split(',')

2.   ["C230719000015", "C230719000015", "C230719000016"]  过滤掉相同的  :

        使用Set数据结构来排除数组中的相同元素。Set是一种无重复值的数据结构,可以很方便地用来去除数组中的重复元素 。

        var arr = ["C230719000015", "C230719000015", "C230719000016"];

        var newArr = [...new Set(arr)];

        console.log(newArr);      //    ["C230719000015", "C230719000016"]

3.  var list = [1,2]      list.push(1)     与list 值相同的    就不push 进去    不相同的push进去:

        var list = [1, 2];

        var element = 1;

        if (!list.includes(element)) {

                list.push(element);

        }

        console.log(list);   //   [1, 2]

4.  map  方法返回一个新数组 

        var list = [

                {extendNo: "C230719000015", ordOrderId: 424992053},

                {extendNo: "C230719000015", ordOrderId: 424992053},

                {extendNo: "C230719000016", ordOrderId: 424992053}

          ];

        var extendNoArr = list.map(obj => obj.extendNo);

        console.log(extendNoArr);   //  ["C230719000015", "C230719000015", "C230719000016"]

5.  var arr = [{id: 1, name: "A"}, {id: 2, name: "B"}, {id: 1, name: "A"}, {id: 3, name: "C"}]

        排除name为c 的返回新的数组

        使用filter()方法来筛选出name不为"C"的元素,从而返回一个新的数组

        var arr = [{id: 1, name: "A"}, {id: 2, name: "B"}, {id: 1, name: "A"}, {id: 3, name: "C"}];

        var newArr = arr.filter(function(element) {

                return element.name !== "C";

        });

        console.log(newArr);​​​​​​​    //   [{id: 1, name: "A"}, {id: 2, name: "B"}, {id: 1, name: "A"}]

6.   var downLoadList=[""] js将数组中 为""的去掉

        var downLoadList = [""];

        var filteredList = downLoadList.filter(function(element) {

                        return element !== "";

        });

        console.log(filteredList);​​​​​​​    //  []

7.  var fillingTechnology = "[\"填充工艺2.0\",\"填充工艺4.0\"]" js 转成 ["填充工艺2.0","填充工艺4.0"]

        var fillingTechnology = "[\"填充工艺2.0\",\"填充工艺4.0\"]";

        var fillingTechnologyArray = JSON.parse(fillingTechnology);      

           console.log(fillingTechnologyArray);​​​​​​​    //   ["填充工艺2.0","填充工艺4.0"]

8. var fillingTechnology = ["填充工艺4.0","无"]

        var a = '填充工艺4.0' ,     判断fillingTechnology 是否包含 a

        var fillingTechnology = ["填充工艺4.0", "无"];

        var a = '填充工艺4.0';

        var isContainsA = fillingTechnology.includes(a);

        console.log(isContainsA);​​​​​​​   //   true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值