Skip to content

Instantly share code, notes, and snippets.

View RienNeVaPlus's full-sized avatar
🔌
Plugged in

RienNeVaPlus RienNeVaPlus

🔌
Plugged in
View GitHub Profile
@RienNeVaPlus
RienNeVaPlus / object.querySelector.ts
Created September 20, 2022 02:19
A querySelector utility to fetch values from an object by providing a path string. `querySelector({a:[{b:3]}, 'a[0].b') === 3`
const querySelectorRegex = /(?<=\[)(?!")[^\]]+|(?<=\[")[^"]+|[^."[\]]+/g
export function objectQuerySelector(obj: any, path: string){
const match = path.match(objectQueryRegex)
return match ? match.reduce((res, prop) => res[prop], obj) : undefined
}