递归,回调和nodejs [英] recursion, callback and nodejs

查看:487
本文介绍了递归,回调和nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的问题,递归,回调和nodejs。



我有这个对象例如:

  var a = [
{index:1},
{index:2},
{index: 11},
{index:12},
{index:[
{index:111},
{index:112},
{index: -of-tree},
]},
]},
{index:4},
]

我想依次显示:

  1 
2
11
12
111
112
树结尾
4

与console.log(值)



在纯javascript中很容易,但是当节点异步运行时,



谁能帮助我?

解决方案

你可以尝试这样更简单的东西!

  function ff(item){
var temp = item.index;
if(temp instanceof Array)temp.forEach(ff);
else console.log(temp);
}
a.forEach(ff);

事实上,你的问题没有异步它是一个数组中的同步itteration! p>

I have a big problem with recursion, callback and nodejs.

I have this object for exemple :

var a= [
    {index : 1},
    {index : 2},
    {index : [
        {index : 11},
        {index : 12},
        {index : [
            {index : 111},
            {index : 112},
            {index : "end-of-tree"},
        ]},
    ]},
    {index : 4},
]

And I would like to display sequentially this:

1
2
11
12
111
112
end-of-tree
4

with console.log(value)

In pure javascript it's easy but as node run asynchronously, it's a bit more complicated.

Who could help me ?

解决方案

You could try something simpler like this!

function ff(item){
  var temp = item.index;
  if(temp instanceof Array) temp.forEach(ff);
  else console.log(temp);   
}
a.forEach(ff);

In fact your problem doesn't have anything asynchronous it is a synchronous itteration in an array!

这篇关于递归,回调和nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆