search

Home  >  Q&A  >  body text

angular.js - How to write recursion in JS or angular?

The picture shown is an object. Each child node has an array of children. The arrays are all combinations of objects in this pattern.

The lowest children.length=0. I need to add objects to a collection according to the nodes in sequence, such as angular's $scope.list=[];

This is what I write now. It works, but the order is wrong. Some nodes are later, but earlier, so the display is wrong:

cycle(data);


        function cycle(projection) {
            console.log("现在第几:"+times,projection.name);
            times += 1;
            if(times < 15) {

                for(var k in projection) {

                    if(k == "children") {

//                        console.log("find k.value is:", projection[k]);

                        if(projection[k].length > 0) {

                            for(var i = 0; i < projection[k].length; i++) {

//                                console.log("size>0:name", projection[k][i].name, projection[k][i]);

                                cycle(projection[k][i]);

                                $scope.dd.push(projection[k][i]);
                            }
                        }
                    }
                }
            } 
        }

        console.log("sfdsfsdfsdfsdf:", $scope.dd);
    });

The requirement is: the nodes of the pyramid must be added to $scope.list.push() in sequence, and the node objects of the third layer cannot be added to it before the objects of the second layer. How to improve this stuff?

怪我咯怪我咯2820 days ago545

reply all(1)I'll reply

  • 習慣沉默

    習慣沉默2017-05-15 17:06:23

    Eliminate zero reply grass grass grass grass grass grass grass grass grass grass grass

    reply
    0
  • Cancelreply