Last active
November 18, 2020 15:35
-
-
Save AlekseyArh/53811ecab8daa83a310215aa47eec1c2 to your computer and use it in GitHub Desktop.
Аналог функции isset() в JavaScript | JavaScript isset() equivalent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Расширяем объект | Extend object | |
Object.prototype.isset = function (path = '') { | |
if (path === '') { | |
return true; | |
} | |
let keys = path.split('.'); | |
return typeof this[keys[0]] === 'undefined' || this[keys[0]] === null ? false : this[keys[0]].isset(keys.slice(1).join('.')); | |
}; | |
// Проверяем наличие первого символа в строке | Check for the first character in the string | |
console.log('abv'.isset('0')) // true | |
// Проверяем ключи | Check the keys | |
let test = { | |
a: { | |
b: [0, 1] | |
} | |
}; | |
// Результаты | Results | |
console.log(test.isset('a.b')) // true | |
console.log(test.isset('a.b.1')) // true | |
console.log(test.isset('a.b.5')) // false | |
console.log(test.isset('a.c')) // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment