[Solved] Iteration through array data


I just coded a node js code, you can still make it work on a browser in js, but you will need to download the underscore library here.

_und = require('underscore');

data = [{
        "id": 1,
        "children": [{
                "id": 7,
                "children": [{
                        "id": 8,
                        "children": [{
                                "id": 4
                            }, {
                                "id": 5
                            }, {
                                "id": 11
                            }
                        ]
                    }, {
                        "id": 9
                    }
                ]
            }, {
                "id": 6
            }, {
                "id": 10
            }
        ]
    }, {
        "id": 2,
        "children": [{
                "id": 3
            }, {
                "id": 12
            }
        ]
    }, {
        "id": 13
    }
]

function parse_tree_2(n) {
    return(_und.map(n, parse_tree));
}

function parse_tree(n) {
    if (n['children']) {
        return({id: n['id'], children: parse_tree_2(n['children'])});
    } else {
        return({id: n['id']});
    }
}

result = _und.map(data, parse_tree);

console.log("Result: %j", result);

You can put that into a file, and execute it with node (by download underscore with nmp install node).

On plain js it would be something like:

<script type="text/javascript" src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript">

data = [{
        "id": 1,
        "children": [{
                "id": 7,
                "children": [{
                        "id": 8,
                        "children": [{
                                "id": 4
                            }, {
                                "id": 5
                            }, {
                                "id": 11
                            }
                        ]
                    }, {
                        "id": 9
                    }
                ]
            }, {
                "id": 6
            }, {
                "id": 10
            }
        ]
    }, {
        "id": 2,
        "children": [{
                "id": 3
            }, {
                "id": 12
            }
        ]
    }, {
        "id": 13
    }
]

function parse_tree_2(n) {
    return(_.map(n, parse_tree));
}

function parse_tree(n) {
    if (n['children']) {
        return({id: n['id'], children: parse_tree_2(n['children'])});
    } else {
        return({id: n['id']});
    }
}

result = _.map(data, parse_tree);

console.log("Result: %j", result);

</script>

Ruby code:

require 'json'

data = <<EOF
[{
        "id": 1,
        "children": [{
                "id": 7,
                "children": [{
                        "id": 8,
                        "children": [{
                                "id": 4
                            }, {
                                "id": 5
                            }, {
                                "id": 11
                            }
                        ]
                    }, {
                        "id": 9
                    }
                ]
            }, {
                "id": 6
            }, {
                "id": 10
            }
        ]
    }, {
        "id": 2,
        "children": [{
                "id": 3
            }, {
                "id": 12
            }
        ]
    }, {
        "id": 13
    }
]
EOF

json = JSON.parse(data)

def parse_tree(n)
  if n["children"]
    {id: n["id"], children: n['children'].map {|c| parse_tree(c)} }
  else
    {id: n["id"]}
  end
end


result = json.map {|n| parse_tree(n) }

puts result

5

solved Iteration through array data