We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function transform(values) { // ... } transform({ 0: { username: '0', department: 'A-B-C', }, 1: { username: '1', department: 'A-B-D', }, 2: { username: '2', department: 'A-X-Y', }, 3: { username: '2', department: 'A-C-B-D-Y', }, }); [ { name: 'A', parent: '', path: 'A', children: [ { name: '0', parent: 'A', path: 'A-B', children: [ { name: '0', parent: 'A-B', path: 'A-B-C', children: [], }, { name: '1', parent: 'A-B', path: 'A-B-D', children: [], }, ], }, { name: '2', parent: 'A', path: 'A-X', children: [ { name: '2', parent: 'A-X', path: 'A-X-Y', children: [], }, ], }, { name: '2', parent: 'A', path: 'A-C', children: [ { name: '2', parent: 'A-C', path: 'A-C-B', children: [ { name: '2', parent: 'A-C-B', path: 'A-C-B-D', children: [ { name: '2', parent: 'A-C-B-D', path: 'A-C-B-D-Y', children: [], }, ], }, ], }, ], }, ], }, ];
The text was updated successfully, but these errors were encountered:
function transform(values) { const res = []; const arr = []; const pathPool = []; for (const key in values) { const {username, department} = values[key]; const paths = department.split('-'); for (let i = 0; i < paths.length; i++) { const path = paths[i]; const curPath = paths.slice(0, i + 1).join('-'); if (pathPool.includes(curPath)) { continue; } pathPool.push(curPath); obj.name = i === 0 ? path : username; obj.path = curPath; obj.parent = i < 1 ? '' : paths.slice(0, i).join('-'); obj.children = []; arr.push(obj); } } const map = {}; for (let item of arr) { map[item.path] = item; } for (let item of arr) { if (!item.parent) { res.push(item); continue; } else { const parent = map[item.parent]; if (parent) { parent.children.push(item); } } } return res; }
Sorry, something went wrong.
No branches or pull requests
题目
The text was updated successfully, but these errors were encountered: