Returns whether a value exists at a given keypath within an object using dot or bracket notation.
Supports keypaths like:
'foo.bar'
'foo[1].spam'
'foo["bar-baz"].quux'
"foo['bar-baz'].quux"
This function cannot consider values associated with symbol keys.
The object to examine the value from
The keypath using dot or bracket notation
true if the keypath exists, false otherwise
true
false
const obj = { foo: { bar: 'hello', 'bar-baz': { quux: 'world' }, }, arr: [{ spam: 'eggs' }],};has(obj, 'foo.bar');has(obj, 'arr[0].spam');has(obj, 'foo["bar-baz"].quux');has(obj, 'foo.nonexistent'); Copy
const obj = { foo: { bar: 'hello', 'bar-baz': { quux: 'world' }, }, arr: [{ spam: 'eggs' }],};has(obj, 'foo.bar');has(obj, 'arr[0].spam');has(obj, 'foo["bar-baz"].quux');has(obj, 'foo.nonexistent');
Returns whether a value exists at a given keypath within an object using dot or bracket notation.
Supports keypaths like:
'foo.bar'- dot notation'foo[1].spam'- bracket notation with array indices'foo["bar-baz"].quux'- bracket notation with quoted strings"foo['bar-baz'].quux"- bracket notation with single quotesThis function cannot consider values associated with symbol keys.