【问题标题】:Javascript list loop/recursion to create an objectJavascript列表循环/递归创建对象
【发布时间】:2020-01-12 03:42:19
【问题描述】:

我正在尝试创建一个函数来使用 JavaScript 中的递归或循环来处理与深度相关的数字列表。

下面的“输入”需要处理成“输出”,它需要对任意列表起作用。

需要注意的一点是,数字会增加 0 或 1,但可能会减少任意数量。

var input = [0, 1, 2, 3, 1, 2, 0]

var output =
  [ { number: 0, children: 
      [ { number: 1, children: 
          [ { number: 2, children: 
              [ { number: 3, children: [] } ]
            } 
          ] 
        } 
      , { number: 1, children: 
          [ { number: 2, children: [] } ]
        } 
      ] 
    } 
  , { number: 0, children: [] } 
  ] 

我自己解决了这个问题,尽管它需要一些改进。

var example = [0, 1, 2, 2, 3, 1, 2, 0]
var tokens = []
var last = 0
const createJSON = (input, output) => {
  if (input[0] === last) {
    output.push({ num: input[0], children: [] })
    createJSON(input.splice(1), output)
  } 
  else if (input[0] > last) {
    last = input[0]
    output.push(createJSON(input, output[output.length-1].children))
  } 
  else if (input[0] < last) {
    var steps = input[0]
    var tmp = tokens
    while (steps > 0) {
      tmp = tmp[tmp.length-1].children
      steps--
    }
    tmp.push({ num: input[0], children: [] })
    createJSON(input.splice(1), tmp)
  }
}
createJSON(example, tokens)
console.log(tokens)

【问题讨论】:

  • I'm trying to create a function 你能告诉我们你是如何尝试实现你的功能的吗?您具体在哪里遇到问题?
  • 您能详细说明您要做什么吗?非常不清楚。
  • 我们希望您在发布之前进行研究并进行尝试。然后,当您发帖时,您将展示您尝试过的内容并就您遇到的问题提出具体问题。我们不是代码编写服务,所以请帮助我们帮助您。
  • 你讨厌在输出对象中放置逗号?
  • 很抱歉最初没有添加我的代码。我没有添加逗号,因为它只是为了表示,我认为它更具可读性。

标签: javascript


【解决方案1】:

其实解决起来很简单……

var input   = [0, 1, 2, 3, 1, 2, 0]
  , output  = []
  , parents = [output]
  ;
for(el of input)
  {
  let nv = { number:el, children:[] }
  parents[el].push( nv )
  parents[++el] = nv.children  // save the  @ddress of children:[] for adding items on
  }
console.log( output )
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 感谢您的回答,比我的解决方案简单得多。这行“父母[++el] = nv.children”是做什么的?
  • @user3462522 你的意思是++el ?这与 C 相同的递增运算符参见 = developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 啊,我知道这个。我只是认为列表中可能会发生一些聪明的事情。我如何学会如此简单地解决这样的递归问题,有推荐的资源或书籍吗?
  • @user3462522 这不是一个递归问题,唯一的问题就是要知道根据其缩进将到达的元素附加到哪个元素,从哪里想到一个表格来保存表格的地址在收集它们之前。解决好问题意味着首先提出正确的问题
【解决方案2】:

这是一个基于递归和Array.prototype.reduce的函数式解决方案:

const data = [0, 1, 2, 3, 1, 2, 0]
const last = xs => xs.length > 0 ? xs[xs.length - 1] : undefined
const lastD = (d, t, i = 0) => i > d ? t : lastD(d, last(t).children, i + 1)
const buildIt = xs => xs.reduce(
  (a, x) =>((x === 0 ? a : lastD(x - 1, a)).push({ number: x, children: [] }), a),
  []
)

console.log(buildIt(data))
.as-console-wrapper { max-height: 100% !important; top: 0; }

注意:此解决方案不依赖于用于记账目的的其他变量的变异。

事实证明,实际问题比我最初的误解要简单得多!

【讨论】:

  • 感谢 Tex,它是正确的,但您的第一个解决方案不能正确处理递减数字。第二个“1”结束于根,而不是前一个“0”的孩子。
  • 啊,我误读了所需的输出格式。以为1是根。明天可能再看看。
  • 已经接近了,我也遇到了减少数字的困难。
  • user3462522 我已经根据您的评论修复了我的 sn-p。
猜你喜欢
  • 1970-01-01
  • 2017-07-18
  • 2011-04-18
  • 2021-05-09
  • 2013-03-19
  • 2016-11-22
  • 1970-01-01
  • 2020-08-08
相关资源
最近更新 更多