I would like to improve this for loop. Ideally I do not want the variable 'nodeFound' outside the function scope and I would like to return the 'nodeFound' as soon as it is found not after the loop has completed.
var nodeFound;
proto._getNodeById = function(id, node) {
var data = node || this._data;
var l = data.length;
var i;
for ( i = 0; i < l; i++) {
if (Number(id) === data[i].id) {
nodeFound = data[i];
} else {
if (data[i].children.length) {
this._getNodeById(id, data[i].children);
}
}
}
return nodeFound;
};