Skip to content

Instantly share code, notes, and snippets.

@AlekseyArh

AlekseyArh/isset.js

Last active Nov 18, 2020
Embed
What would you like to do?
Аналог функции 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