对数组的一些操作js

1、复制一行表格的数据

//this.optForm.agreementList是表格的数据

 
handleCope(scope){
      this.$confirm('确定复制吗', '提示').then(() => {
        let arr = this.optForm.agreementList.slice(scope.$index, scope.$index + 1)[0];
        // 注意:转成json再反转回来,不能直接使用赋值,不然会和复制的一层公用同一个内存单元地址,会得到一样的结果
        let obj = JSON.stringify(arr);
        let newObj = JSON.parse(obj);
        // 这里可以对复制的数据进行操作
        newObj.agreeId = ''; 
        newObj.status = 1; 
       this.optForm.agreementList.splice(scope.$index + 1, 0, newObj);
      })
    },

2、删除一行表格数据

handleDelete(scope){
       this.$confirm('确定删除吗', '提示').then(() => {
        this.optForm.agreementList.splice(scope.$index,1);
      })
    },

3、数组中两个元素换位置

//页面布局
<ul>
       <li v-for="(item,index) in arr" :key="index">
         <div class="zxlist_box">
            <div><span class="fa">发</span><strong>{{item.name}}</strong></div>
            <div class="martop10">{{item.address}}</div>
            <div class="martop10">{{item.detail}}</div>
         </div>
         <div class="rightarrow" @click="changeArr(index)"><i class="el-icon-refresh"></i></div>
       </li>
</ul>


//定义数组
arr:[
          {id:"01",name:"1号",address:'地址1地址1地址1地址1',detail:"12方,10吨"},
          {id:"02",name:"2号",address:'地址2',detail:"11方,123吨"},
          {id:"03",name:"3号",address:'地址3',detail:"19方,111吨"},
          {id:"04",name:"4号",address:'地址4',detail:"212方,5吨"},
        ],

//切换方法
changeArr(val){
      this.arr[val] = this.arr.splice(val+1, 1, this.arr[val])[0];
},

4、提取数组对象中的某些属性值并赋值给新的数组对象

fList=[{
  planAddrId:9,
  address:'哈哈哈',
  ordCount:3,
  rtnGoods:false,
},
{
  planAddrId:10,
  address:'嗯嗯嗯',
  ordCount:6,
  rtnGoods:true,
}]



let paraSlist= this.fList.map(item=>{
        var data = {
                    planAddrId: item.planAddrId,
                    rtnGoods: item.rtnGoods,
                    } 
        return data
      })
//把planAddrId修改成aaa
paraSlist=paraSlist.map(item=>{
        return {aaa:item.planAddrId}
      })

console.log(paraSlist)

//打印出来的结果
paraSlist=[{
  aaa:9,
  rtnGoods:false,
},
{
  aaa:10,
  rtnGoods:true,}]

//把planAddrId修改成aaa,rtnGoods修改成bbb
paraSlist=paraSlist.map(item=>{
       var data={
          aaa: item.planAddrId,
          bbb: item.rtnGoods,
        }
        return data
      })

console.log(paraSlist)

//打印出来的结果
paraSlist=[{
  aaa:9,
  bbb:false,
},
{
  aaa:10,
  bbb:true,}]

5、数组对象中查数据

        let arr=[
					{name:'张三',age:'28',sex:'男'},
					{name:'张三',age:'19',sex:'女'},
					{name:'王五',age:'23',sex:'女'},
					{name:'张雪丽',age:'27',sex:'男'},
					{name:'夏玉婷',age:'28',sex:'女'}
				];

				//找到名字为张三的数组
				let bb=[];
				arr.forEach((item,index)=>{
					if(item.name=='张三'){
						bb.push(item)
					}
				})
			 console.log(bb)
				
				//找到名字为张三的索引
				let aa=[];
				arr.forEach((item,index)=>{
					if(item.name=='张三'){
						aa.push(index)
					}
				})
			 console.log(aa)


				//找到名字为张三的各自的年龄
				let cc;
				arr.forEach((item,index)=>{
					if(item.name=='张三'){
					 cc=arr[index].age
					}
				})
			 console.log(cc)


				//找到年纪大于25岁的人
				let dd=[];
				arr.forEach((item,index)=>{
					if(item.age>25){
					 dd.push(arr[index].name)
					}
				})
			 console.log(dd)


				//判断李四是否在这个数组里面
				let ee=	arr.findIndex(item=>{
					return item.name=='李四'
				})
				console.log(ee)

6、判断数组中的元素是否相等

从数组中获取所有的contractId号,push到一个新的数组contractIdList

let rst = contractIdList.every(item => contractIdList.every(item2 => item2 == item ? true : false));
if (rst) {
    //数组元素的值相等代码块
   } else {
      //数组元素的值不相等,给错误提示
      console.error("error")
   }

7、一维数组变多维数组。

/**
        * resParentid为父id,最顶层的父id为0
        * resId为自己id
       list参数为获取一维数组
       tree参数为操作后获得的结果数据 **/
      handleData(list,tree){
        for (let i = 0; i < list.length; i++) {
            let item = list[i]
            if (item.resParentid == "0") {
                let item = list[i]
                item.children = []
                tree.push(item)
                list.splice(i, 1)
                i--
            }
        }
        setChild(tree)
        function setChild(tree) {
            if (list.length > 0) {
                for (let i = 0; i < tree.length; i++) {
                    for (let j = 0; j < list.length; j++) {  
                        if (list[j].resParentid == tree[i].resId) {
                            let item = list[j]
                            item.children = []
                            tree[i].children.push(item)
                            list.splice(j, 1)
                            j--
                        }
                    }
                    setChild(tree[i].children)
                }
            }
        }
      },


//查看权限详情
     lookDetail(id){
       let perIds=id!==""?id:this.dataListRight.map(item=>item.perId).join(',')
       let param={
         perIds:perIds,
         mdcKey:this.topInfo.mdcKey
       }
       this.$api.role.selectResListByPerIds(param).then(res => {
            if (res.data.code == "0") {
              console.log(res.data.data)
              this.dataAll=[];
              this.$nextTick(()=>{
                this.handleData(res.data.data,this.dataAll)
              })
            }
            else{
              this.$message({type: "error", message: res.data.msg});
            }
          })
          .catch(error => {
            this.btnLoading = false;
       });
     },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~犇犇~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值