let obj = {
"viewport" : {
"key" : {
"lat" : 47,
"lng" : 39
},
"southwest" : {
"lat" : 47,
"lng" : 39
},
"one" : {
"keygen" : 47,
"lng" : 39
}
}
}
let obj = {
"viewport" : {
"key" : {
"lat" : 47,
"lng" : 39
},
"southwest" : {
"lat" : 47,
"lng" : 39
},
"one" : {
"keygen" : 47,
"lng" : 39
}
}
}
function deepSearch (object, key, predicate) {
if (object.hasOwnProperty(key)) {
return object
}
for (let i = 0; i < Object.keys(object).length; i++) {
if (typeof object[Object.keys(object)[i]] === 'object') {
let o = deepSearch(object[Object.keys(object)[i]], key)
if (o != null) return o
}
}
return null
}
const result = deepSearch(obj, 'keygen');
console.log(result); // {keygen: 47, lng: 39}
const obj = {
a: {
b: {
c: 1
}
}
};
if (obj?.a?.b?.c) {
console.log('Ключ "c" существует');
} else {
console.log('Ключ "c" не существует');
}