As long as they are not afraid of circular references, you can do the following
function findX(obj) { var val = obj['x']; if (val !== undefined) { return val; } for (var name in obj) { var result = findX(obj[name]); if (result !== undefined) { return result; } } return undefined; }
Note. This will search for the “x” property directly in this object or prototype chain. If you specifically want to limit the search to this object, you can do the following:
if (obj.hasOwnProperty('x')) { return obj['x']; }
And repeat for findX recursive call findX
source share