You need to use recursion.
function get_all_ids(data) {
var result = [];
$(data).each(function (i, element) {
result.push(element.attr.id);
if (element.children && element.children.length > 0) {
var ids = get_all_ids(element.children);
result = result.concat(ids); // or $.merge(result, ids);
}
});
return result;
}