1棘催、判斷兩個對象數(shù)組是否有重復(fù)的元素倦西?
// 兩數(shù)組是否有重復(fù)元素
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
// 當(dāng)前時間格式化
moment().format('YYYY-MM-DD HH:mm:ss')
3旅急、處理 tree 組件 通過 id 獲取父級節(jié)點的數(shù)據(jù)
// 根據(jù)id 獲取父級節(jié)點的數(shù)據(jù)
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 根據(jù)當(dāng)前 id 獲取當(dāng)前節(jié)點數(shù)據(jù)
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)
}
})
}
}
}
}