Dynamically get a nested value from an array or object with a string.
const person = { name: "John", friends: [{ name: "Jane" }],}; get(person, "friends[0].name"); // => 'Jane'
npx atmx add helper get
Copy and paste the following method into @/utils/helpers/undefined.ts:
@/utils/helpers/undefined.ts
/*** Dynamically get a nested value from an array or object with a string.** @example* const person = {* name: 'John',* friends: [{ name: 'Jane' }]* }** get(person, 'friends[0].name') // => 'Jane'*/export function get<TDefault = unknown>(value: any,path: string,defaultValue?: TDefault,): TDefault {const segments = path.split(/[\.\[\]]/g); let current: any = value; for (const key of segments) {if (current === null || current === undefined) { return defaultValue as TDefault;} const dequoted = key.replace(/['"]/g, ""); if (dequoted.trim() === "") { continue;} current = current[dequoted];} if (current === undefined) {return defaultValue as TDefault;} return current;}