Breadth first traversal of object

前端 未结 3 1397
南旧
南旧 2021-02-02 04:47

I am making a program that solves a puzzle game, and it finds all the possible moves on a board and puts all the possible resulting boards in an object. Then it finds all the po

3条回答
  •  遥遥无期
    2021-02-02 05:13

    Not completely tested:

    var oo = {
        board: {
            starts: [[0,0],[0,3]],
            blocks: [[3,0],[3,3]],
            ends:   [[2,4]]
        },
        possibleMoves: [{
            board: {
                starts: [[0,0],[2,3]],
                blocks: [[3,0],[3,3]],
                ends:   [[2,4]]
            },
        }],
    };
    
    
    function traverseObject (o) {
        for (var prop in o) {
            if (typeof o[prop] == "array" || typeof o[prop] == "object") {
                traverseObject(o[prop]);
                console.log(prop);
            } else {
                console.log(prop, "=", o[prop]);
            }
        }
    }
    
    traverseObject(oo);
    

提交回复
热议问题