将平行结构转换为文件树结构

  1. var arr = [
  2. {
  3. id: 0,
  4. parentId: null,
  5. children: [],
  6. },
  7. {
  8. id: 1,
  9. parentId: 0,
  10. children: [],
  11. },
  12. {
  13. id: 2,
  14. parentId: 0,
  15. children: [],
  16. },
  17. {
  18. id: 3,
  19. parentId: 1,
  20. children: [],
  21. },
  22. {
  23. id: 4,
  24. parentId: 1,
  25. children: [],
  26. },
  27. {
  28. id: 5,
  29. parentId: 1,
  30. children: [],
  31. },
  32. {
  33. id: 6,
  34. parentId: 1,
  35. children: [],
  36. },
  37. {
  38. id: 7,
  39. parentId: 2,
  40. children: [],
  41. },
  42. {
  43. id: 8,
  44. parentId: 2,
  45. children: [],
  46. },
  47. {
  48. id: 9,
  49. parentId: 2,
  50. children: [],
  51. },
  52. {
  53. id: 10,
  54. parentId: 2,
  55. children: [],
  56. },
  57. ];
  58. function multiArray(arr) { //一维转多维
  59. const newArr = [];
  60. arr.forEach(function (item, index) {
  61. if (item.parentId === null) {
  62. const obj = {};
  63. Object.assign(obj, item);
  64. newArr.push(obj);
  65. }
  66. arr.forEach(function (item2, index2) {
  67. if (item.id === item2.parentId) {
  68. const obj = {};
  69. Object.assign(obj, item2);
  70. item.children.push(obj);
  71. }
  72. })
  73. })
  74. return newArr[0];
  75. }
  76. function flattenArray(arr) { //多维转一维
  77. var newArr = [];
  78. return (function fn(arr) {
  79. if (Object.prototype.toString.call(arr) === '[object Object]') {
  80. var att = arr.children && arr.children.length ? JSON.parse(JSON.stringify(arr.children)) : [];
  81. arr.children = [];
  82. newArr.push(arr);
  83. fn(att);
  84. } else {
  85. arr.forEach(function (item, index) {
  86. console.log(item.children.length, 66)
  87. if (item.children.length) {
  88. var att = JSON.parse(JSON.stringify(item.children));
  89. item.children = [];
  90. newArr.push(item);
  91. fn(att);
  92. } else {
  93. newArr.push(item);
  94. }
  95. })
  96. }
  97. return newArr;
  98. })(arr)
  99. }
  100. // 这两个函数有副作用,会对外层arr造成影响
  101. console.log(multiArray(arr));
  102. console.log(flattenArray(multiArray(arr)));

没有函数副作用的语法

  1. function AssignToNew(a) {
  2. // 没有副作用的
  3. function toNew(target, source) {
  4. const keys = Object.keys(source);
  5. keys.forEach((key) => {
  6. if (typeof source[key] === 'object') {
  7. if (Array.isArray(source[key])) {
  8. target[key] = [];
  9. toNew(target[key], source[key]);
  10. } else if (source[key] === null) {
  11. target[key] = null;
  12. } else if (source[key] === undefined) {
  13. target[key] = undefined;
  14. } else {
  15. target[key] = {};
  16. toNew(target[key], source[key]);
  17. }
  18. } else {
  19. target[key] = source[key]
  20. }
  21. });
  22. }
  23. if (typeof a === 'object') {
  24. if (Array.isArray(a)) {
  25. obj = [];
  26. } else if (a === null) {
  27. obj = null;
  28. } else if (a === undefined) {
  29. obj = undefined;
  30. } else {
  31. obj = {};
  32. }
  33. toNew(obj, a)
  34. } else {
  35. obj = a;
  36. }
  37. return obj;
  38. }
  39. function multiArray(list) { //一维转多维
  40. const arr = AssignToNew(list);
  41. const newArr = [];
  42. arr.forEach(function (item, index) {
  43. if (item.parentId === null) {
  44. const obj = {};
  45. Object.assign(obj, item);
  46. newArr.push(obj);
  47. }
  48. arr.forEach(function (item2, index2) {
  49. if (item.id === item2.parentId) {
  50. const obj = {};
  51. Object.assign(obj, item2);
  52. item.children.push(obj);
  53. }
  54. })
  55. })
  56. return newArr[0];
  57. }
  58. function flattenArray(list) { //多维转一维
  59. const arr = AssignToNew(list);
  60. var newArr = [];
  61. return (function fn(arr) {
  62. if (Object.prototype.toString.call(arr) === '[object Object]') {
  63. var att = arr.children && arr.children.length ? JSON.parse(JSON.stringify(arr.children)) : [];
  64. arr.children = [];
  65. newArr.push(arr);
  66. fn(att);
  67. } else {
  68. arr.forEach(function (item, index) {
  69. console.log(item.children.length, 66)
  70. if (item.children.length) {
  71. var att = JSON.parse(JSON.stringify(item.children));
  72. item.children = [];
  73. newArr.push(item);
  74. fn(att);
  75. } else {
  76. newArr.push(item);
  77. }
  78. })
  79. }
  80. return newArr;
  81. })(arr)
  82. }
  83. console.log(multiArray(arr));
  84. console.log(flattenArray(multiArray(arr)));

Array赋值的注意事项与循环方式的差异

  1. var a = [];
  2. a['1'] = 2;
  3. a['3'] = 3;
  4. a['b'] = 4;
  5. console.log('a.length:', a.length);
  6. for (let i = 0; i < a.length; i += 1) {
  7. console.log('for:', a[i]);
  8. }
  9. for (let i in a) {
  10. console.log('for in:', i, a[i]);
  11. }
  12. a.forEach((a) => {
  13. console.log('forEach:', a);
  14. });
  15. /*
  16. VM3641:5 a.length: 4
  17. VM3641:7 for: undefined
  18. VM3641:7 for: 2
  19. VM3641:7 for: undefined
  20. VM3641:7 for: 3
  21. VM3641:11 for in: 1 2
  22. VM3641:11 for in: 3 3
  23. VM3641:11 for in: b 4
  24. VM3641:14 forEach: 2
  25. VM3641:14 forEach: 3
  26. */