Skip to content

Instantly share code, notes, and snippets.

@AlekseyArh
Last active November 18, 2020 15:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlekseyArh/53811ecab8daa83a310215aa47eec1c2 to your computer and use it in GitHub Desktop.
Save AlekseyArh/53811ecab8daa83a310215aa47eec1c2 to your computer and use it in GitHub Desktop.
Аналог функции isset() в JavaScript | JavaScript isset() equivalent
// Расширяем объект | 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