For objects, it's currently very difficult (if not impossible) to have access to the key and the value in the same scope of the expression. This function would decompose objects into Array<[key:value]> such that every key corresponds to a decomposed value.
In javascript (typescript) this might look something like:
registerFunction(
'flatMapValues',
([inputObject]) => {
return Object.entries(inputObject).reduce((flattened, entry) => {
const [key, value]: [string, any] = entry;
if (Array.isArray(value)) {
return [...flattened, ...value.map(v => [key, v])];
}
return [...flattened, [key, value]];
}, [] as any[]);
},
[{ types: [TYPE_OBJECT, TYPE_ARRAY] }],
);
Which produces the following:
jmespath.search( { a: [1, 3, 5], b: [2, 4, 6] }, "flatMapValues(@)")
// OUTPUTS: [
// ['a', 1],
// ['a', 3],
// ['a', 5],
// ['b', 2],
// ['b', 4],
// ['b', 6],
// ]
jmespath.search({ a: [true, { x: 3 }, null, 1234, ['XXX']], b: { x: 2 } }, "flatMapValues(@)")
// OUTPUTS: [
// ['a', true],
// ['a', { x: 3 }],
// ['a', null],
// ['a', 1234],
// ['a', ['XXX']],
// ['b', { x: 2 }],
// ]
jmespath.search([ [1, 3, 5], [2, 4, 6] ], "flatMapValues(@)")
// OUTPUTS: [
// ['0', 1],
// ['0', 3],
// ['0', 5],
// ['1', 2],
// ['1', 4],
// ['1', 6],
// ]
These could then be piped to expressions that filter on keys or project keys into new objects/arrays
For objects, it's currently very difficult (if not impossible) to have access to the key and the value in the same scope of the expression. This function would decompose objects into Array<[key:value]> such that every key corresponds to a decomposed value.
In javascript (typescript) this might look something like:
Which produces the following:
These could then be piped to expressions that filter on keys or project keys into new objects/arrays