array.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * author: linqian
  3. * description: 数组操作函数库
  4. * date: 2021-07-09 09:50
  5. */
  6. // 从树形数据中递归筛选目标值
  7. function valInDeep(arr = [], val, id, childs) {
  8. return arr.reduce((flat, item) => {
  9. return flat.concat(item[id] == val ? item : valInDeep(item[childs] || [], val, id, childs));
  10. }, []);
  11. }
  12. // 将树形数据向下递归为一维数组
  13. function flattenDeep(arr = [], childs) {
  14. return arr.reduce((flat, item) => {
  15. return flat.concat(item, item[childs] ? flattenDeep(item[childs], childs) : [] );
  16. }, []);
  17. }
  18. // 将树形数据向上将此支线递归为一维数组
  19. function flattenDeepParents(arr, parent) {
  20. return arr.reduce((flat, item) => {
  21. return flat.concat(item[parent] || [], item[parent] ? flattenDeepParents([item[parent]], parent) : [] );
  22. }, []);
  23. }
  24. // 根据条件递归祖先元素
  25. function regDeepParents(row, parent, cb) {
  26. if(row[parent]){
  27. cb && cb(row[parent]);
  28. regDeepParents(row[parent], parent, cb);
  29. }
  30. }
  31. export { valInDeep, flattenDeep, flattenDeepParents, regDeepParents }