1、数组对象去重
this.polluteNumOptions = [ { remark: 'aa', name: 'test' }, { remark: 'bba', name: 'test1' }, { remark: 'aa', name: 'test' } ]export function removedup(arr, batch) { if (!Array.isArray(arr)) { return arr; } if (arr.length == 0) { return []; } let obj = {}; let uniqueArr = arr.reduce(function (total, item) { obj[item[batch]] ? '' : (obj[item[batch]] = true && total.push(item)); return total; }, []); return uniqueArr; } this.polluteNumOptions = removedup(this.polluteNumOptions, 'remark')
let obj = {}; this.polluteNumOptions = this.polluteNumOptions.reduce((cur, next) => {obj[next.remark] ? '' : (obj[next.remark] = true && cur.push(next));return cur;}, []);
2、数组筛选
const list = List.filter(item => { if (item.id === 'xx') { return item } })
3、数组字典格式化
export function formateDict(k, list, type = 0, joinName = ',',isLeaf="0", catchAction="") { /** * 描述:字典值格式化 在表格显示val值 * k 字典k值 * list 字典列表值 * type 0 判定为字符串不需要分割 1 需要分割 * joinName 多个字符串的时候join的字符 * isLeaf 0 不显示叶子节点 1只显示叶子节点 * catchAction 强制断点 */ // if (catchAction){ // console.log(k); // return // } if (k == null || typeof k == 'undefined') return '' // console.log(k); if (type == 0) { let valList = list.filter(item => { return item.k == k }); return valList.length == 1 && valList[0].val ? valList[0].val : '字典错误'; } // 判断是否包含分隔符 , 、 - | const keyList = [',', '-', '、', '|']; var val = ""; var code = 0; keyList.map(keyname => { if (code == 0) if (k.indexOf(keyname) > -1) { var arr_k = k.split(keyname); // 判断是否显示叶子节点 if(isLeaf ==1) { arr_k = getAllSon(arr_k, list); } var arr_val = []; arr_k.map(key => { let valList = list.filter(item => { return item.k == key }); if(valList.length == 0) { valList = [{val: '其他'}]; } let val = valList.length == 1 && valList[0].val ? valList[0].val : '字典错误'; arr_val.push(val); }) // all_val 去重 保留一个其他 arr_val = [...new Set(arr_val)] val = arr_val.length > 0 ? arr_val.join(joinName) : '字典错误'; if (val != '字典错误') code = 1; } else { // 可能只有一条k let valList = list.filter(item => { return item.k == k }); val = valList.length == 1 && valList[0].val ? valList[0].val : '字典错误'; if (val != '字典错误') code = 1; } }) return val }
4、tree获取所有子节点 / 子节点获取所有父级节点
export function getAllSon(arr, list) { /**去掉所有的不是子节点叶子 */ let arrSonx = []; const getSonList = (arrSon) => { arrSon.map(item=> { if(item.children) { getSonList(item.children) } else { arrSonx.push(item.k) } }) return arrSonx; } let arrSonAll = getSonList(list) // 开始去掉不是子节点的 return arr.filter(item=>{ return arrSonAll.includes(item) }) }export function getFamily(array, family, id) { /** * get father data by child */ for (var i = 0; i < array.length; i++) { var item = array[i]; if (item.id == id) { family.push(item.id); return family; } if (item.child && item.child.length > 0) { family.push(item.id); var rs = getFamily(item.child, family, id); if (rs) { return rs; } else { family.remove(item.id); } } } return false; }
5、list根据pid转换成tree
export function makeTree(arr, dictList, key = 'pid', type = 0) { /*** * 需要组装成tree的list * 所有的数据 dict * 标识父级的key * type 0 开启权限判断(生产线-product_line)子集只有生产线 1 不开启权限判断 */ const institutionalScope = null; arr.map(item_arr => { if (type != 0 || institutionalScope == null || institutionalScope == '') { let arr = dictList.filter(item => { return item[key] == item_arr.id }) if (arr.length > 0) item_arr.children = dictList.filter(item => { return item[key] == item_arr.id }); } else { item_arr.children = key == 'product_line' ? dictList.filter(item => { return item[key] == item_arr.id && institutionalScope.indexOf(item.id) > -1 }) : dictList.filter(item => { return item[key] == item_arr.id }) } if (item_arr.children && item_arr.children.length > 0) makeTree(item_arr.children, dictList) }) }
6、字典筛选 根绝key值转换成汉字
export function filterDict(keyList = [], dictList, type = 0, popVal = "", remark = "", remarkType = 0) { /** * 描述:筛选字典 返回dict对象 * keyList 字典k值list [key1, key2, key1&&key2] * dictList 字典列表值 * type 0 开启权限判断(目前只判断车间-workshop及生产线-product_line) 1 不开启权限判断 * popVal 排除某个值 通过val判断 * remark 筛选remark值 * remarkType 精确筛选 1 模糊筛选0 */ const institutionalScope = null; if (keyList.length == 0) return [] var arr = {}; keyList.map(key => { // key 包含&& 则会合并 if (key.indexOf('&&') == -1) { if (type != 0 || institutionalScope == null || institutionalScope == '') { arr[key] = dictList.filter(item => { // if (remark == '') // return item.type.trim() == key && item.val != popVal // else // return item.type.trim() == key && item.val != popVal && (remarkType==0? item.remark.indexOf(remark) > -1 : item.remark == remark) return remark == '' ? item.type.trim() == key && item.val != popVal : item.type.trim() == key && item.val != popVal && (remarkType == 0 ? item.remark.indexOf(remark) > -1 : item.remark == remark) }); } else { arr[key] = key == 'workshop' || key == 'product_line' ? dictList.filter(item => { // if (remark == '') // return item.type.trim() == key && institutionalScope.indexOf(item.id) > -1 && item.val != popVal // else // return item.type.trim() == key && institutionalScope.indexOf(item.id) > -1 && item.val != popVal && (remarkType==0? item.remark.indexOf(remark) > -1 : item.remark == remark) return remark == '' ? item.type.trim() == key && institutionalScope.indexOf(item.id) > -1 && item.val != popVal : item.type.trim() == key && institutionalScope.indexOf(item.id) > -1 && item.val != popVal && (remarkType == 0 ? item.remark.indexOf(remark) > -1 : item.remark == remark) }) : dictList.filter(item => { // if (remark == '') // return item.type.trim() == key && item.val != popVal // else // return item.type.trim() == key && item.val != popVal && (remarkType==0? item.remark.indexOf(remark) > -1 : item.remark == remark) return remark == '' ? item.type.trim() == key && item.val != popVal : item.type.trim() == key && item.val != popVal && (remarkType == 0 ? item.remark.indexOf(remark) > -1 : item.remark == remark) }) } } else { // 包含多个key的话 进行合并 key1&&key2 let keyArr = key.split('&&'); arr[key] = []; keyArr.map(item_k => { dictList.filter(item => { if (type != 0 || institutionalScope == null || institutionalScope == '') { if (remark == '') { if (item.type.trim() == item_k && item.val != popVal) arr[key] = [...arr[key], item] } else { if (item.type.trim() == item_k && item.val != popVal && (remarkType == 0 ? item.remark.indexOf(remark) > -1 : item.remark == remark)) arr[key] = [...arr[key], item] } } else { if (key == 'workshop' || key == 'product_line') { if (remark == '') { if (item.type.trim() == item_k && institutionalScope.indexOf(item.id) > -1 && item.val != popVal) arr[key] = [...arr[key], item] } else { if (item.type.trim() == item_k && institutionalScope.indexOf(item.id) > -1 && item.val != popVal && (remarkType == 0 ? item.remark.indexOf(remark) > -1 : item.remark == remark)) arr[key] = [...arr[key], item] } } else { if (remark == '') { if (item.type.trim() == item_k && item.val != popVal) arr[key] = [...arr[key], item] } else { if (item.type.trim() == item_k && item.val != popVal && (remarkType == 0 ? item.remark.indexOf(remark) > -1 : item.remark == remark)) arr[key] = [...arr[key], item] } } } }); }) } // 去重 var obj = {}; arr[key] = arr[key].reduce(function (item, next) { obj[next.val] || obj[next.k] ? '' : obj[next.val] = true && item.push(next); return item; }, []); makeTree(arr[key], dictList) }) return arr; }
标签:&&,remark,key,val,arr,用法,item,快捷,数组 From: https://www.cnblogs.com/shuangzikun/p/taotao_array_obj_tool.html