Instead of strings, you can pass needles as arrays of integers (for array indices) and strings (for object keys).
Benefits:
- Matches array keys with integers and object keys with strings.
- Does not support other matching syntax (no wildcards/regex).
- Does not require escaping.
- Parses faster than string needles.
- Allows the
key result from objectScan to be passed back into itself.
Note: matchedBy and similar options contain the original needles, not copies. Use useArraySelector: false if you want to avoid the [n] notation in the output.
// Mixed path: 'a', then index 0, then 'b'
const haystack1 = { a: [{ b: 0 }] };
objectScan([['a', 0, 'b']], { joined: true })(haystack1);
// => [ 'a[0].b' ]
// Using array needles with 'rtn: value'
const haystack2 = { 'a.b': [0], a: { b: [1] } };
objectScan([['a.b', 0]], { joined: true, rtn: 'value' })(haystack2);
// => [ 0 ]
// Mixed needles (array needles + string wildcard needle)
const haystack3 = { a: [{ b: 0 }, { b: 0 }] };
objectScan([['a', 0, 'b'], ['a', 1, 'b'], 'a[*].b'], { joined: true, rtn: 'matchedBy' })(haystack3);
// => [ [ [ 'a', 1, 'b' ], 'a[*].b' ], [ [ 'a', 0, 'b' ], 'a[*].b' ] ]