commonFunctions.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. export function addArrayObjectNodeIterative(list,key, targetValue, updateData) {
  2. // 1. 初始化栈,将根数组放入
  3. const stack = [...list];
  4. while (stack.length > 0) {
  5. const node = stack.pop();
  6. // 2. 检查当前节点
  7. if (node?.[key] === targetValue) {
  8. console.log('执行了')
  9. let children = node?.children || []
  10. children.push(
  11. {
  12. label:updateData,
  13. id:targetValue + `${children?.length+1}`,
  14. children:[]
  15. }
  16. )
  17. // console.log('children',children)
  18. Object.assign(node, {...node,children});
  19. return true;
  20. }
  21. // 3. 将子节点推入栈中(如果有)
  22. if (node.children && node.children.length) {
  23. stack.push(...node.children);
  24. }
  25. }
  26. return false;
  27. }
  28. export function updateArrayObjectNodeIterative(list,key, targetValue, updateData) {
  29. // 1. 初始化栈,将根数组放入
  30. const stack = [...list];
  31. while (stack.length > 0) {
  32. const node = stack.pop();
  33. // 2. 检查当前节点
  34. if (node?.[key] === targetValue) {
  35. console.log('执行了')
  36. Object.assign(node, {...node,label:updateData});
  37. return true;
  38. }
  39. // 3. 将子节点推入栈中(如果有)
  40. if (node.children && node.children.length) {
  41. stack.push(...node.children);
  42. }
  43. }
  44. return false;
  45. }
  46. export function replaceArrayObjectNodeKeyVal(list,key, targetKey) {
  47. // 1. 初始化栈,将根数组放入
  48. const stack = [...list];
  49. while (stack.length > 0) {
  50. const node = stack.pop();
  51. Object.assign(node, {...node,[key]:node?.[targetKey]});
  52. // 3. 将子节点推入栈中(如果有)
  53. if (node.children && node.children.length) {
  54. stack.push(...node.children);
  55. }
  56. }
  57. return false;
  58. }
  59. // 会修改原数组
  60. export function addNodeKeyValOfArrayObject(list,key,targetVal){
  61. // 1. 初始化栈,将根数组放入
  62. const stack = [...list];
  63. while (stack.length > 0) {
  64. const node = stack.pop();
  65. Object.assign(node, {...node,[key]:targetVal});
  66. // 3. 将子节点推入栈中(如果有)
  67. if (node.children && node.children.length) {
  68. stack.push(...node.children);
  69. }
  70. }
  71. }
  72. export function removeNodeById(list, key,targetValue) {
  73. return list.filter(node => {
  74. // 1. 如果当前节点就是目标,直接过滤掉(返回 false)
  75. if (node?.[key] === targetValue) {
  76. return false;
  77. }
  78. // 2. 如果当前节点不是目标,但有子节点,则递归处理子节点
  79. if (node.children && node.children.length) {
  80. node.children = removeNodeById(node.children,key, targetValue);
  81. }
  82. // 3. 保留当前节点
  83. return true;
  84. });
  85. }
  86. export function extractArrayObjectNode(list,key,matchKeysValue){
  87. let finalResult:any[] = []
  88. // 1. 初始化栈,将根数组放入
  89. const stack = [...list];
  90. while (stack.length > 0) {
  91. const node = stack.pop();
  92. if(matchKeysValue.includes(node?.[key])){
  93. finalResult.push(node)
  94. }else if(node?.children && node.children?.length){
  95. let filterResult = extractArrayObjectNode(node?.children,key,matchKeysValue)
  96. if(filterResult?.length){
  97. finalResult.push({
  98. ...node,
  99. children:filterResult
  100. })
  101. }
  102. }
  103. }
  104. return finalResult
  105. }
  106. export function extractBlurObjectNode(list,key,matchKeysValue){
  107. let finalResult:any[] = []
  108. // 1. 初始化栈,将根数组放入
  109. const stack = [...list];
  110. while (stack.length > 0) {
  111. const node = stack.pop();
  112. if(new RegExp(matchKeysValue).test(node?.[key])){
  113. finalResult.push(node)
  114. }else if(node?.children && node.children?.length){
  115. let filterResult = extractBlurObjectNode(node?.children,key,matchKeysValue)
  116. if(filterResult?.length){
  117. finalResult.push({
  118. ...node,
  119. children:filterResult
  120. })
  121. }
  122. }
  123. }
  124. return finalResult
  125. }
  126. export function extractArrayObjectLeafKey(list,key,extraKey?:any){
  127. let aimKeys = []
  128. const stack = [...list]
  129. while(stack.length > 0){
  130. let node = stack.pop()
  131. if(node.children && node.children.length > 0){
  132. stack.push(...(node.children))
  133. }else{
  134. if(extraKey){
  135. let keysObj = {}
  136. extraKey.map(item=>{
  137. keysObj[item] = node?.[item]
  138. })
  139. let params = JSON.stringify({
  140. [key]:node?.[key],
  141. ...keysObj,
  142. // [extraKey]:node?.[extraKey]
  143. })
  144. aimKeys.push(params)
  145. }else{
  146. aimKeys.push(node?.[key])
  147. }
  148. }
  149. }
  150. // 需要执行去重
  151. let result = [...(new Set(aimKeys))]
  152. if(extraKey){
  153. return result?.map(item=>{
  154. return JSON.parse(item || '{}')
  155. })
  156. }else{
  157. return result
  158. }
  159. }
  160. export const deepSort = (list, key='code', childrenKey = 'children')=>{
  161. let sortList = list.sort((a,b)=>{
  162. return a?.[key] - b?.[key]
  163. })
  164. let finalList = sortList.map(item=>{
  165. if(item.children?.length > 1){
  166. let children = deepSort(item?.[childrenKey])
  167. return {
  168. ...item,
  169. [childrenKey]:children
  170. }
  171. }else{
  172. return item
  173. }
  174. })
  175. return finalList
  176. }