首页 \ 问答 \ Javascript递归,foreach循环不会退出?(Javascript recursion, foreach loop won't exit?)

Javascript递归,foreach循环不会退出?(Javascript recursion, foreach loop won't exit?)

我构建了一个简单的Javascript树视图,其中每个节点都有一个名称,一个孩子列表和一个ID:

class Node {
  constructor(name, childNodes, id) {
    this.name = name;
    this.childNodes = childNodes;
    this.id = id;
  }
}

我的目标是创建一个函数“getNodeById(parent,id)”,它返回具有给定ID的节点。 我曾尝试使用递归方法,但在某处必定会出现错误:

function getNodeById(currentNode, id) {
  if (currentNode.id === id) { console.log("found"); return currentNode; }
  currentNode.children.forEach(child => {
    console.log(child);
    return getNodeById(child, id);
  });
}

我的想法是,该函数将搜索childNodes并再次调用自己。 当找到正确的节点时,应该在if语句后返回,然后在foreach循环内返回。 该函数成功找到正确的节点,但不会停止并返回节点。 这是调用getNodeById时的输出(parent,2);

在这里输入图像描述

为什么函数在“找到”后退出?


I build a simple Javascript treeview, where every Node has a name, a list of children and an ID:

class Node {
  constructor(name, childNodes, id) {
    this.name = name;
    this.childNodes = childNodes;
    this.id = id;
  }
}

My Aim is to create a function "getNodeById(parent, id)", that returns the Node with the given ID. I have tried using a recursive method, but there has to be a mistake somewhere:

function getNodeById(currentNode, id) {
  if (currentNode.id === id) { console.log("found"); return currentNode; }
  currentNode.children.forEach(child => {
    console.log(child);
    return getNodeById(child, id);
  });
}

My idea is, that the function will search through the childNodes and call itsself again. When the the correct node is found, it should be returned after the if-statement, then inside the foreach loop. The function finds the right node successfully but it won't stop and return the Node. Here is the output when calling getNodeById(parent, 2);

enter image description here

Why doesn't the function exit after the "found"?


原文:https://stackoverflow.com/questions/50025579
更新时间:2022-02-28 21:02

最满意答案

Mark Haris有关于优化Cuda的非常好的演示文稿: http//developer.download.nvidia.com/compute/cuda/1.1-Beta/x86_website/projects/reduction/doc/reduction.pdf

Algorithmic optimizations
Changes to addressing, algorithm cascading
11.84x speedup, combined!
Code optimizations
Loop unrolling
2.54x speedup, combined

有一个额外的操作语句确实会引起问题,尽管它是你想要优化的最后一件事,如果不是因为你需要在实现大小假设之前知道代码的布局!

您正在处理的问题听起来像n-body问题,请参阅http://http.developer.nvidia.com/GPUGems3/gpugems3_ch31.html

如果您可以避免进行成对计算,则可以实现额外的性能提升,例如,元素距离太远而不能相互影响。 这适用于任何可以几何表达的关系,无论是成对成本还是弹簧物理模拟。 我最喜欢的方法是将网格划分为多个盒子,并且每个元素通过划分放入一个盒子中,然后仅评估相邻盒子之间的两两关系。 这可以称为O(n * m)。


Mark Haris has a very good presentation about optimizing Cuda: http://developer.download.nvidia.com/compute/cuda/1.1-Beta/x86_website/projects/reduction/doc/reduction.pdf

Algorithmic optimizations
Changes to addressing, algorithm cascading
11.84x speedup, combined!
Code optimizations
Loop unrolling
2.54x speedup, combined

Having an extra operations statement, does indeed cause problems although it will be the last thing you want to optimize, if not simply because you need to know the layout of your code before implementing the size assumptions!

The problem you are working on sounds like famous the n-body problem, see http://http.developer.nvidia.com/GPUGems3/gpugems3_ch31.html

An additional performance increase can be achieved if you can avoid doing a pairwise computation, for example, the elements are too far to have an effect on each-other. This applies to any relationship that can be expressed geometrically, whether it be pairwise costs or a physics simulation with springs. My favorite method is to divide the grid into boxes and, with each element putting itself into a box via division, then only evaluate pairwise relations between between neighboring boxes. This can be called O(n*m).

相关问答

更多
  • http://dev.mysql.com/doc/refman/5.0/en/mysqlcheck.html 这是数据库管理系统需要担心的问题。 你无法对同步做任何事情。 http://dev.mysql.com/doc/refman/5.0/en/mysqlcheck.html This is something that the database management system needs to worry about. You can't really do anything about sync ...
  • 请在下次提出单独的问题。 我无法回答这个问题。 我不太确定我理解这个问题。 不同的配置文件可以包含给定插件的不同值。 通常,如果你激活所有这些,只有其中一个会赢。 建议仅使用Pom父母poms。 实际上,他们是保持理智的唯一方法。 我们有很多运行selenium的经验,但只有通过surefire-plugin(以及jetty-plugin),因为我们所有的测试都是基于junit的。 它的效果非常好。 Please ask separate questions next time. I cannot answ ...
  • Mark Haris有关于优化Cuda的非常好的演示文稿: http : //developer.download.nvidia.com/compute/cuda/1.1-Beta/x86_website/projects/reduction/doc/reduction.pdf Algorithmic optimizations Changes to addressing, algorithm cascading 11.84x speedup, combined! Code optimizations Lo ...
  • 石英 Quartz使用自定义线程调度程序( org.quartz.core.QuartzScheduler ),它使用java系统时间。 它可以集成commonj接口为JEE(WAS和Weblogic)可互操作。 重新加载配置:阅读Quartz:如何使用org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin重新加载作业和触发器? Spring批处理管理控制台用于春季批处理,是监视批处理活动的打包 重新加载配置使用API 一般来说,您可以通过编程方式使用 ...