Repository navigation
Replies: 1 comment
|
There's no wildcard support. For a single import { get } from 'es-toolkit/compat';
const obj = { a: [{ b: 'c' }, { b: 'd' }, { c: 'e' }] };
const values = get(obj, 'a', [])
.map(item => get(item, 'b'))
.filter(v => v !== undefined);
// ['c', 'd']If you need arbitrary paths like import { get } from 'es-toolkit/compat';
function getAll(obj: unknown, path: string): unknown[] {
const [head, ...rest] = path.split('[*]');
const base = head ? get(obj, head) : obj;
if (rest.length === 0) return base === undefined ? [] : [base];
if (!Array.isArray(base)) return [];
const restPath = rest.join('[*]').replace(/^\./, '');
return base.flatMap(item => (restPath ? getAll(item, restPath) : [item]));
}
getAll(obj, 'a[*].b'); // ['c', 'd']If you need the full JSONPath syntax (filters etc.), something like |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hi I was wondering if there was a native way to get all data that follows a path with [*]. Here is an example of what I am looking for:
I have ways around, but was wondering there there was something like this.
All reactions