关于vue.js:ES6-常用数据处理及Vueelement应用场景

1.获取雷同ID下的对象的值
利用场景;vue+element select 回显在table中解决 || 后盾传label值回显的时候

export function mapOptionLabel(data, value) {
  let item = data.find(item => item.value === value);
  if (item) {
    return item.label;
  }
  return "-";
}

2.过滤掉两个数组中对象id值相等的项
办法1

let arr1=[{id:1,name:'老大'},{id:2,name:'老二'}]
let arr2=[{id:1,name:'老大'},{id:3,name:'老三'},{id:4,name:'老四'},{id:5,name:'老五'}]
let add=arr2.filter(item=>!arr1.some(ele=>ele.id===item.id))

办法2

const arr1 = [{ id: 1, name: '老大' }, { id: 2, name: '老二' }];

const arr2 = [{ id: 1, name: '老大' }, { id: 3, name: '老三' }];

// 获取到arr1的所有id汇合

let arr1Ids = arr1.map(item => item.id);

// 过滤arr2中不蕴含arr1雷同的id数组。

const result = arr2.filter(item => !arr1Ids.includes(item.id));

console.log(result);

3.element table表格回显数据

let ids = this.statementIds.split(',') // a

let tableId = [] // b

this.tableData.forEach(res=>{ //把表格里所有的id退出到b里

tableId.push(res.billId.toString())

})

for(let i = 0;i<tableId.length;i++){ //对b循环 如果b外面有a(a是被选中的行) 则把索引代入到first函数里 让这些行的复选框选中

if(tableId.indexOf(ids[i])>=0){

this.first([this.tableData[tableId.indexOf(ids[i])]])

}

}

4.校验含有某个字符串

['create', 'edit'].includes(pageAction)
5.过滤两个数组中雷同项,生成一个新数组
办法1

let a = [a, b, c, d, e];
let b = [c, d, f, g, h];
    
let result = b.filter(item => !a.some(e => e === item));
//result: [f, g, h]

办法2

a.filter(item=>b.indexOf(item)==-1)

6.element,tree构造回显未抉择的数据
我司后盾给了两个数组对象,tree构造一个b;选中的数据为a
须要比照b中id和a中id相等的时候过滤掉

a = [{id:1}]
b = [{id:1,name : 'name'}]
`result = b.filter(item => !a.some(ele=>ele.id===item.id));`

7.ES6数组排重在从新排序

var arr=[1,3,3,5,9,4,6,7];
let s=new Set(arr1);
let arr = [....s];

consolo.log(arr);

//打印进去的是1,3,4,5,6,7,9

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理