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
let data = [ { "name": "1-1", "kind": "oo", "children": [ { "name": "2-2", "kind": "ii", }, { "name": "3-3", "children": [ { "name": "4-4", "children": [ { "name": '707' } ] } ] } ] }, { "name": "5-5", }, { "name": "6-6" } ] function getName(data, key = "name") { let result = []; if (Array.isArray(data)) { for (let item of data) { if (item[key]) { result.push(item[key]); } if (item.children) { result = result.concat(getName(item.children)); } } return result; } else { return result; } } console.log(getName(data))
The text was updated successfully, but these errors were encountered:
function getName(data, key = 'name') { const result = [] const dfs = (node) => { for (const p of node) { p.name && result.push(p[key]) p.children && dfs(p.children) } } Array.isArray(data) && dfs(data) return result }
Sorry, something went wrong.
function getName(data) { let res = [], len = data.length for (let i = 0; i < len; i++) { traversal(data[i]) } return res function traversal(node) { if (!node) return res.push(node.name) node.children && node.children.forEach(item => { traversal(item) }) } }
No branches or pull requests
The text was updated successfully, but these errors were encountered: