简体   繁体   English

从树形结构中获取所有“名称”

[英]Grabbing all the “names” from a tree structure

There are endless amount of tree questions on here, but for me, it would be nice if someone could help me with this specific question. 这里有无数的树问题,但是对我来说,如果有人可以帮助我解决这个特定问题,那将是很好的。 I took a "mock interview" for my boot camp and this was a question. 我参加了新兵训练营的“模拟面试”,这是一个问题。 I did not have a good way to approach it.. 我没有一个很好的方法来解决它。

const people = {
    name: "Robin",
    children: [
        {
            name: "Alberto",
            children: [
                {
                    name: "Quinn",
                    children: [
                        {
                            name: "Conner",
                            children: []
                        },
                        {
                            name: "Lila",
                            children: []
                        }
                    ]
                }
            ]
        },
        {
            name: "Charlie",
            children: []
        }
    ]
}

// Write a function called getNames that returns a string "Robin, Alberto, Quinn, Conner, Lila, Charlie

It can be a little overwhelming when you're trying to learn this stuff, especially when you're sifting through a multitude of questions that have different angles to solving them. 当您尝试学习这些东西时,可能会有些不知所措,尤其是当您在筛选具有不同角度解决问题的大量问题时。 So, help on this particular one would be greatly appreciated! 因此,非常感谢您对此特定的帮助!

You could use reduce method to create recursive function that will return a string. 您可以使用reduce方法创建将返回字符串的递归函数。

 const people = {"name":"Robin","children":[{"name":"Alberto","children":[{"name":"Quinn","children":[{"name":"Conner","children":[]},{"name":"Lila","children":[]}]}]},{"name":"Charlie","children":[]}]} function getNames(data, name = "") { return data.name + (data.children ? data.children.reduce((r, e) => { return r + ", " + getNames(e) }, "") : "") } console.log(getNames(people)) 

Easy, just use recursion. 简单,只需使用递归即可。

 const people = { name: "Robin", children: [ { name: "Alberto", children: [ { name: "Quinn", children: [ { name: "Conner", children: [] }, { name: "Lila", children: [] } ] } ] }, { name: "Charlie", } ]}; function getNames(tree) { var names = []; for (var i = 0; i < tree.length; i++) { names.push(tree[i].name) if (tree[i].children) { names = names.concat(getNames(tree[i].children)) } } return names; } var names = getNames([people]); console.log(names); 

Recursive data structures can be easily transformed using the new flatMap 可以使用新的flatMap轻松地转换递归数据结构

 const people = { name: "Robin", children: [ { name: "Alberto", children: [ { name: "Quinn", children: [ { name: "Conner", children: [] }, { name: "Lila", children: [] } ] } ] }, { name: "Charlie", } ]}; const getNames = ({ name, children = [] }) => [ name, ...children.flatMap(getNames) ] console.log(getNames(people)) // [ "Robin", "Alberto", "Quinn", "Conner", "Lila", "Charlie" ] 

If your environment doesn't define it, you can write your own flatMap 如果您的环境未定义,则可以编写自己的flatMap

 const flatMap = (f, xs = [], context = null) => xs.reduce ( (acc, x, i) => acc.concat (f.call (context, x, i, xs)) , [] ) const people = { name: "Robin", children: [ { name: "Alberto", children: [ { name: "Quinn", children: [ { name: "Conner", children: [] }, { name: "Lila", children: [] } ] } ] }, { name: "Charlie", } ]}; const getNames = ({ name, children = [] }) => [ name, ...flatMap (getNames, children) ] console.log(getNames(people)) // [ "Robin", "Alberto", "Quinn", "Conner", "Lila", "Charlie" ] 

flatMap is supported in Node 11, but no longer included in babel-polyfill as of Babel 7, where it now has to be manually imported 节点11支持flatMap ,但从Babel 7开始不再包含在babel-polyfill中,现在必须手动将其导入

// Node 10 or Babel 7
import 'core-js/fn/array/flat-map'

Of you can polyfill it manually 您可以手动填充

 // manual polyfill Array.prototype.flatMap = function (f, context = null) { return this.reduce ( (acc, x, i) => acc.concat (f.call (context, x, i, this)) , [] ) } const people = { name: "Robin", children: [ { name: "Alberto", children: [ { name: "Quinn", children: [ { name: "Conner", children: [] }, { name: "Lila", children: [] } ] } ] }, { name: "Charlie", } ]}; const getNames = ({ name, children = [] }) => [ name, ...children.flatMap(getNames) ] console.log(getNames(people)) // [ "Robin", "Alberto", "Quinn", "Conner", "Lila", "Charlie" ] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM