跳到主要内容
CNode

求问各位前辈一个面试问题

问答
FFantasyGao发布于 9 年前最后回复 9 年前
341220

写出给定DOM的深度,大家有什么好的写法没,稍微具体一下,比递归深一点的。大神不吝赐教啊

来自酷炫的 CNodeMD

查看回复

回复 (3)

I
ianchn#39 年前
引用 FantasyGao@ianchn 大神厉害了,愣是看不懂 来自酷炫的 CNodeMD

@FantasyGao 不是我写的,看原地址,这个是比递归深一点的匿名函数递归。。。

I
ianchn#19 年前

https://stackoverflow.com/questions/33903929/iterate-throught-html-dom-and-get-depth

补充: LZ 想要的可能是下面这个

(f =>
  (x => f(y => x(x)(y)))
  (x => f(y => x(x)(y)))
)
  (get =>
    node =>
      Array.from(node.childNodes)
        .filter(node => node.nodeType === Node.ELEMENT_NODE)
        .reduce((depth, node) => Math.max(depth, get(node) + 1), 1)
  )
  (document.documentElement)

原地址

参与回复

登录后即可参与回复。登录