recursion, callback and nodejs

2019-06-08 18:35发布

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 ?

3条回答
神经病院院长
2楼-- · 2019-06-08 18:47

I manage to get it to work.

JSON exemple :

var a= [
    {index : 1},
    {index : 2},
    {index : [
        {index : 11},
        {index : 12},
        {index : [
            {index : 111},
            {index : 112},
            {index : "end-of-tree 1xx"},
        ]},
    ]},
    {index : 4},
    {index : [
        {index : 51},
        {index : 52},
        {index : [
            {index : 531},
            {index : 532},
            {index : [
                {index : 5341},
                {index : 5342},
                {index : [
                    {index : 53441},
                    {index : 53442},
                    {index : "end-of-tree 5xx"},
                ]}, 
            ]},
        ]},
    ]},
    {index : 6},
    {index : 7},
    {index : 8},
    {index : 9},    
    {index : [
        {index : 'A0'},
        {index : 'A1'},
        {index : [
            {index : 'A21'},
            {index : 'A22'},
            {index : [
                {index : 'A311'},
                {index : 'A312'},
                {index : [
                    {index : 'A3131'},
                    {index : 'A3132'},
                    {index : "end-of-tree Axx"},
                ]}, 
            ]},
        ]},
    ]}, 
]

Javascript code :

function tree(json) {
    item=json[0];
    if (item) {
        if (Array.isArray(item.index)) {
            tree(item.index);
        } else {
            console.log(item.index)
            json.shift();
            if (json) tree(json);
        }       
    } else {
        a.shift();
        if (a.length>0) tree(a);
    }
} 

tree(a)

Console Log result :

1
2
11
12
111
112
end-of-tree 1xx
4
51
52
531
532
5341
5342
53441
53442
end-of-tree 5xx
6
7
8
9
A0
A1
A21
A22
A311
A312
A3131
A3132
end-of-tree Axx
查看更多
贼婆χ
3楼-- · 2019-06-08 19:08

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!

查看更多
Bombasti
4楼-- · 2019-06-08 19:08

@alex-rocabillis.... https://github.com/oracle/node-oracledb/blob/master/doc/api.md#-523-execute

oracle execute fonction is asynchronous. I gonna create an other post to explain the problem.

5.2.3 execute()

Prototype

void execute(String sql, [Object bindParams, [Object options,]] function(Error error, [Object result]){}); Return Value

None

Description

This call executes a SQL or PL/SQL statement. See SQL Execution for examples.

This is an asynchronous call.

The statement to be executed may contain IN binds, OUT or IN OUT bind values or variables, which are bound using either an object or an array.

A callback function returns a result object, containing any fetched rows, the values of any OUT and IN OUT bind variables, and the number of rows affected by the execution of DML statements.

Parameters

查看更多
登录 后发表回答