javascript
search field inside object tree:
var a = {a: {b: {c: null}}}; const DEEP_LIMIT = 100; let alloverProps = 0; function searchAttr(obj, title, deep, arrIgnore){ console.log(deep || 0) if (obj[title] !== (alloverProps++, void 0)) return [obj[title], title]; if (DEEP_LIMIT < (deep || 0)) return false; for (let p in obj){ if (typeof obj[p] === 'object' && obj[p] !== null ){ if (arrIgnore && Array.isArray(obj[p])) continue; else{ let res = searchAttr(obj[p], title, (deep || 0) + 1, arrIgnore) if (res){ let [r, path] = res if (r !== void 0) return [r, p+'->' + path]; } } } } } console.log(JSON.stringify(searchAttr(a, 'a'))) console.log(JSON.stringify(searchAttr(a, 'c'))) /// output: // 0 // [{"b":{"c":null}},"a"] // 0 // 1 // 2 // [null,"a->b->c"]
(ваш голос учтен)