deepGet
is compatible to call with Array
Definition
function deepGet(...keys: string[]) : any throws Error;
function deepGetP(...keys: string[]) : any | undefined;
Example
As the name implies, goes ballsdeep into an object to get values associated with a key or a list of nested keys
import '@marthvon/protopp';
// or
import '@marthvon/protopp/objectpp/methods/deepSet.js';
const o = { a: { b: 1 } };
console.log(o.deepGet('a', 'b'));
// Outputs 1
console.log(o.deepGet('a', 'c'));
// Error!!! key ācā doesn't exists
// Unless...
console.log(o.deepGetP('a', 'c'));
// Outputs undefined
So what's the difference?... deepGet will throw and error when a key or the nested keys doesn't exist in the object. While, deepGetP will return undefined
console.log(o.deepGetP('a', 'b', 'c'));
// Outputs undefined given value of key b isn't even an object
Why you may ask?...
a?.b?.c; // Same
a.deepGetP('b', 'c'); // Same
The focus is more for inputing a list of string keys to get a value instead of it being hardcoded in as the first one. The strings keys can be dynamic and assigned at runtime, which is an effective tool in some cases.
a['b']['c']; // Same
a.deepGet('b', 'c'); // Same but throws an Error consistently when missing key
And, in terms of the first one, I think I prefer latter more.