1、判断两个对象数组是否有重复的元素?
// 两数组是否有重复元素
const arrFilter = (arrA: any, arrB: any) => {
const newA = new Set(arrA)
const newB = new Set(arrB)
const intersectionSet = new Set([...newA].filter(x => newB.has(x)))
console.log(intersectionSet)
const arr = Array.from(intersectionSet)
return arr
}
2、格式化日期 eg: 2022-11-29 15:51 :30
// 当前时间格式化
moment().format('YYYY-MM-DD HH:mm:ss')
3、处理 tree 组件 通过 id 获取父级节点的数据
// 根据id 获取父级节点的数据
const getparentDataById = (list: any, id: any): any => {
for (let i in list) {
if (list[i].id == id) {
return [list[i]]
}
if (!!list[i].children && list[i].children.length > 0) {
const find = getparentDataById(list[i].children, id)
if (find) {
return find.concat(list[i])
}
}
}
}
4、处理 tree 根据当前 id 获取当前节点数据
const findNodeFromTreeById = (root: any, id: string) => {
if (!!root) {
let type = Object.prototype.toString.call(root).slice(8, -1)
if (type === 'Object') {
if (root.id && root.id === id) {
return root
} else {
let node = root.children || null
findNodeFromTreeById(node, id)
}
} else if (type === 'Array') {
let needNode = root.find((x: any) => !!x === true && x.id === id)
if (!!needNode) {
return needNode
} else {
root &&
root.forEach((item: any) => {
if (item && item.children && item.children.length) {
findNodeFromTreeById(item.children, id)
}
})
}
}
}
}
网友评论