| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- export function addArrayObjectNodeIterative(list,key, targetValue, updateData) {
- // 1. 初始化栈,将根数组放入
- const stack = [...list];
- while (stack.length > 0) {
- const node = stack.pop();
- // 2. 检查当前节点
- if (node?.[key] === targetValue) {
- console.log('执行了')
- let children = node?.children || []
- children.push(
- {
- label:updateData,
- id:targetValue + `${children?.length+1}`,
- children:[]
- }
- )
- // console.log('children',children)
- Object.assign(node, {...node,children});
- return true;
- }
- // 3. 将子节点推入栈中(如果有)
- if (node.children && node.children.length) {
- stack.push(...node.children);
- }
- }
- return false;
- }
- export function updateArrayObjectNodeIterative(list,key, targetValue, updateData) {
- // 1. 初始化栈,将根数组放入
- const stack = [...list];
- while (stack.length > 0) {
- const node = stack.pop();
- // 2. 检查当前节点
- if (node?.[key] === targetValue) {
- console.log('执行了')
- Object.assign(node, {...node,label:updateData});
- return true;
- }
- // 3. 将子节点推入栈中(如果有)
- if (node.children && node.children.length) {
- stack.push(...node.children);
- }
- }
- return false;
- }
- export function replaceArrayObjectNodeKeyVal(list,key, targetKey) {
- // 1. 初始化栈,将根数组放入
- const stack = [...list];
- while (stack.length > 0) {
- const node = stack.pop();
- Object.assign(node, {...node,[key]:node?.[targetKey]});
- // 3. 将子节点推入栈中(如果有)
- if (node.children && node.children.length) {
- stack.push(...node.children);
- }
- }
- return false;
- }
- // 会修改原数组
- export function addNodeKeyValOfArrayObject(list,key,targetVal){
- // 1. 初始化栈,将根数组放入
- const stack = [...list];
- while (stack.length > 0) {
- const node = stack.pop();
- Object.assign(node, {...node,[key]:targetVal});
- // 3. 将子节点推入栈中(如果有)
- if (node.children && node.children.length) {
- stack.push(...node.children);
- }
- }
- }
- export function removeNodeById(list, key,targetValue) {
- return list.filter(node => {
- // 1. 如果当前节点就是目标,直接过滤掉(返回 false)
- if (node?.[key] === targetValue) {
- return false;
- }
- // 2. 如果当前节点不是目标,但有子节点,则递归处理子节点
- if (node.children && node.children.length) {
- node.children = removeNodeById(node.children,key, targetValue);
- }
- // 3. 保留当前节点
- return true;
- });
- }
- export function extractArrayObjectNode(list,key,matchKeysValue){
- let finalResult:any[] = []
- // 1. 初始化栈,将根数组放入
- const stack = [...list];
- while (stack.length > 0) {
- const node = stack.pop();
- if(matchKeysValue.includes(node?.[key])){
- finalResult.push(node)
- }else if(node?.children && node.children?.length){
- let filterResult = extractArrayObjectNode(node?.children,key,matchKeysValue)
- if(filterResult?.length){
- finalResult.push({
- ...node,
- children:filterResult
- })
- }
- }
- }
- return finalResult
- }
- export function extractBlurObjectNode(list,key,matchKeysValue){
- let finalResult:any[] = []
- // 1. 初始化栈,将根数组放入
- const stack = [...list];
- while (stack.length > 0) {
- const node = stack.pop();
- if(new RegExp(matchKeysValue).test(node?.[key])){
- finalResult.push(node)
- }else if(node?.children && node.children?.length){
- let filterResult = extractBlurObjectNode(node?.children,key,matchKeysValue)
- if(filterResult?.length){
- finalResult.push({
- ...node,
- children:filterResult
- })
- }
- }
- }
- return finalResult
- }
- export function extractArrayObjectLeafKey(list,key,extraKey?:any){
- let aimKeys = []
- const stack = [...list]
- while(stack.length > 0){
- let node = stack.pop()
- if(node.children && node.children.length > 0){
- stack.push(...(node.children))
- }else{
- if(extraKey){
- let keysObj = {}
- extraKey.map(item=>{
- keysObj[item] = node?.[item]
- })
- let params = JSON.stringify({
- [key]:node?.[key],
- ...keysObj,
- // [extraKey]:node?.[extraKey]
- })
- aimKeys.push(params)
- }else{
- aimKeys.push(node?.[key])
- }
- }
- }
- // 需要执行去重
- let result = [...(new Set(aimKeys))]
- if(extraKey){
- return result?.map(item=>{
- return JSON.parse(item || '{}')
- })
- }else{
- return result
- }
- }
- export const deepSort = (list, key='code', childrenKey = 'children')=>{
- let sortList = list.sort((a,b)=>{
- return a?.[key] - b?.[key]
- })
- let finalList = sortList.map(item=>{
- if(item.children?.length > 1){
- let children = deepSort(item?.[childrenKey])
- return {
- ...item,
- [childrenKey]:children
- }
- }else{
- return item
- }
- })
- return finalList
- }
|