1. JS从一个数组中删除另一个数组中存在的元素
项目中实例:过滤下拉选项(过滤选择过的数据)
过滤代码参考:
/** * 选择下拉选项
* params {} */ selectChange({ value }) { base.getitemlist(value).then(res => { this.tableData = res.content; // [data1,data2,data3,data4] this.filterEvent() // [data1,data2] }) }, /** * 过滤已添加数据 */ filterEvent() { if (this.filterData === null) return this.tableData = this.tableData.filter(x => !this.filterData.some(e => e.name === x.name)) // [data3,data4] }
/** * 存储对应树节点已存在内容 * params: path,type * path: (层级) {27,32,55,81} * type: 类型 root | children | self * retrun: 返回返回对应的行数据 */ forEachPathElement(path, type) { if (!path) return let pathArr = path.replace(‘{‘, ‘‘).replace(‘}‘, ‘‘).split(‘,‘) if (type == ‘root‘) pathArr = pathArr.slice(0, 1) else if (type == ‘self‘) pathArr = pathArr.slice(0, -1) if (pathArr.length <= 0) return let filterData = [...this.treeData] for (let index = 0; index < pathArr.length; index++) { const item = parseInt(pathArr[index]) if (index == 0) { filterData = filterData.find(e => e.id === item) } else { filterData = filterData.children.find(e => e.id === item) } } if (type != ‘root‘) filterData = filterData.children return filterData }
原文:https://www.cnblogs.com/kizuna1314/p/14822578.html