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

时间:2018-09-19 18:25:25

标签: javascript recursion tree

这里有无数的树问题,但是对我来说,如果有人可以帮助我解决这个特定问题,那将是很好的。我参加了新兵训练营的“模拟面试”,这是一个问题。我没有一个很好的方法来解决这个问题。

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

当您尝试学习这些东西时,可能会有些不知所措,尤其是当您筛选与解决它们的角度不同的众多问题时。因此,非常感谢您对此特定的帮助!

3 个答案:

答案 0 :(得分:2)

您可以使用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))

答案 1 :(得分:1)

容易,只需使用递归即可。

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);

答案 2 :(得分:1)

使用新的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" ]

如果您的环境未定义,则可以编写自己的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在节点11中受支持,但从Babel 7开始不再包含在babel-polyfill中,现在它必须是manually imported

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

其中您可以手动填充

// 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" ]