数组 .some .find .findIndex .map .filter等应用

项目中,经常会使用各种数组方法,用来实现各种功能。

本文简单总结一下:.some .find .findIndex等方法的简单使用。

一.数组中方法的使用

1.some .find使用

selectList:[
          {id:1,arrList:"planList",type:"策划组"},
          {id:2,arrList:"operationsList",type:"运营组"},
      ]
      
 //判断某个组限选10项
   if(this.limitSize(this.selectList,10)){
          return
      }

2.方法封装:


limitSize(arr,size){
          let flag=arr.some((item) => item.arrList&&this[`${item.arrList}`].length>size)
          if (flag) {
                  let obj=arr.find((items) => {if(this[`${items.arrList}`].length>size){ return items} })
                  let showText=obj.type+"不能超过"+size+"个"
                  this.$Message.error(showText);
            }
          return flag
    },

3.findIndex的使用

 //获取到编辑数据的下标
 this.editIndex = this.tableList.findIndex(item => item.service == this.editForm.service)

4.filter的使用

//获取到数组属性匹配后的数组
let getSelObj = this.tableList.filter(item => item.service == this.editForm.service);

5.map的使用

//只返回某个属性的数组
let getSelList = this.tableList.filter(item => item.assId != "").map(item => item.assId)

6.includes的使用

//数组中是否包含某个属性
!fippId.includes(this.editLocalform.assId)

7.push的使用

//符合某种条件的数据存入
setList.push(this.editLocalform);

8.concat的使用

 //数组缀加合并
 this.localServiceList = setList.concat(oldArr);

9.splice的使用

//删除数组的指定某项
 this.localServiceList.splice(this.localServiceList.length - 1, 1);

10.split的使用

//获取到数组集合,并且以为指定空格或标点分割
this.secretKeyList = List.split(",");

11.pop的使用

//删除掉数组的最末尾项
nel.pop()

12.sort的使用

//排序(升序)
 arr.sort((a, b) =>{
          return b.name < a.name ? 1 : -1
 })

13.unshift的使用

//置顶
    sortFirst(index) {
       this.tableList.unshift(this.tableList.splice(index , 1)[0]);
       this.tableList = [...this.tableList];
    },

14.shift的使用

//删除数据第一个
nel.shift()

15.reverse的使用

//反转数组顺序
nel.reverse()

16.contains()的使用

//判断是否包含某个属性
 classList.contains(className)

17.every()的使用

//返回true或false
classes.every(className => classList.contains(className))

18.join()的使用

//以...方式拼接
 localScanning = list.join(',');

19.indexOf()的使用

//是否存在
resetExceptForms(condition,exceptArr) {
      if(condition){
          for(let [k , v] of Object.entries(condition)){
            if(exceptArr&&exceptArr.indexOf(k) === -1) {
                      condition[k]="";
            }
          }
      }
    },

20.lastindexOf()的使用

//找到元素最后一次出现的下标
 let arr = [10,9,8,9,21,2,6];
 arr.lastIndexOf(2) //5

你可能感兴趣的:(Js,Vue,功能,javascript,数组,filter,map,some,every,vue)