将平行结构转换为文件树结构
var arr = [ { id: 0, parentId: null, children: [], }, { id: 1, parentId: 0, children: [], }, { id: 2, parentId: 0, children: [], }, { id: 3, parentId: 1, children: [], }, { id: 4, parentId: 1, children: [], }, { id: 5, parentId: 1, children: [], }, { id: 6, parentId: 1, children: [], }, { id: 7, parentId: 2, children: [], }, { id: 8, parentId: 2, children: [], }, { id: 9, parentId: 2, children: [], }, { id: 10, parentId: 2, children: [], },];function multiArray(arr) { //一维转多维 const newArr = []; arr.forEach(function (item, index) { if (item.parentId === null) { const obj = {}; Object.assign(obj, item); newArr.push(obj); } arr.forEach(function (item2, index2) { if (item.id === item2.parentId) { const obj = {}; Object.assign(obj, item2); item.children.push(obj); } }) }) return newArr[0];}function flattenArray(arr) { //多维转一维 var newArr = []; return (function fn(arr) { if (Object.prototype.toString.call(arr) === '[object Object]') { var att = arr.children && arr.children.length ? JSON.parse(JSON.stringify(arr.children)) : []; arr.children = []; newArr.push(arr); fn(att); } else { arr.forEach(function (item, index) { console.log(item.children.length, 66) if (item.children.length) { var att = JSON.parse(JSON.stringify(item.children)); item.children = []; newArr.push(item); fn(att); } else { newArr.push(item); } }) } return newArr; })(arr)}// 这两个函数有副作用,会对外层arr造成影响console.log(multiArray(arr));console.log(flattenArray(multiArray(arr)));
没有函数副作用的语法
function AssignToNew(a) { // 没有副作用的 function toNew(target, source) { const keys = Object.keys(source); keys.forEach((key) => { if (typeof source[key] === 'object') { if (Array.isArray(source[key])) { target[key] = []; toNew(target[key], source[key]); } else if (source[key] === null) { target[key] = null; } else if (source[key] === undefined) { target[key] = undefined; } else { target[key] = {}; toNew(target[key], source[key]); } } else { target[key] = source[key] } }); } if (typeof a === 'object') { if (Array.isArray(a)) { obj = []; } else if (a === null) { obj = null; } else if (a === undefined) { obj = undefined; } else { obj = {}; } toNew(obj, a) } else { obj = a; } return obj;}function multiArray(list) { //一维转多维 const arr = AssignToNew(list); const newArr = []; arr.forEach(function (item, index) { if (item.parentId === null) { const obj = {}; Object.assign(obj, item); newArr.push(obj); } arr.forEach(function (item2, index2) { if (item.id === item2.parentId) { const obj = {}; Object.assign(obj, item2); item.children.push(obj); } }) }) return newArr[0];}function flattenArray(list) { //多维转一维 const arr = AssignToNew(list); var newArr = []; return (function fn(arr) { if (Object.prototype.toString.call(arr) === '[object Object]') { var att = arr.children && arr.children.length ? JSON.parse(JSON.stringify(arr.children)) : []; arr.children = []; newArr.push(arr); fn(att); } else { arr.forEach(function (item, index) { console.log(item.children.length, 66) if (item.children.length) { var att = JSON.parse(JSON.stringify(item.children)); item.children = []; newArr.push(item); fn(att); } else { newArr.push(item); } }) } return newArr; })(arr)}console.log(multiArray(arr));console.log(flattenArray(multiArray(arr)));
Array赋值的注意事项与循环方式的差异
var a = [];a['1'] = 2;a['3'] = 3;a['b'] = 4;console.log('a.length:', a.length);for (let i = 0; i < a.length; i += 1) { console.log('for:', a[i]);}for (let i in a) { console.log('for in:', i, a[i]);}a.forEach((a) => { console.log('forEach:', a);});/*VM3641:5 a.length: 4VM3641:7 for: undefinedVM3641:7 for: 2VM3641:7 for: undefinedVM3641:7 for: 3VM3641:11 for in: 1 2VM3641:11 for in: 3 3VM3641:11 for in: b 4VM3641:14 forEach: 2VM3641:14 forEach: 3*/