Skip to content

Instantly share code, notes, and snippets.

@PierBover
Last active August 6, 2021 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PierBover/b582a229e30b2edb368587635a057e4d to your computer and use it in GitHub Desktop.
Save PierBover/b582a229e30b2edb368587635a057e4d to your computer and use it in GitHub Desktop.
FQL function to convert the result of an index with values, to an object with the field names
CreateFunction({
name: 'GetIndexValuesObject',
body: Query(
Lambda(
['indexRef', 'indexValuesArray'],
Let(
{
keys: Let(
{
indexDoc: Get(Var('indexRef')),
indexValues: Select('values', Var('indexDoc'))
},
Map(
Var('indexValues'),
Lambda(
'fieldObj',
Let(
{
array: Select('field', Var('fieldObj')),
length: Count(Var('array')),
lastIndex: Subtract(Var('length'), 1)
},
Select(Var('lastIndex'), Var('array'))
)
)
)
),
keysWithIndexes: Reduce(
Lambda(
['accumulator', 'value'],
Append([[Var('value'), Count(Var('accumulator'))]], Var('accumulator'))
),
[],
Var('keys')
)
},
Map(
Var('indexValuesArray'),
Lambda(
'values',
ToObject(
Map(
Var('keysWithIndexes'),
Lambda(
'keyWithIndex',
Let(
{
key: Select(0, Var('keyWithIndex')),
index: Select(1, Var('keyWithIndex')),
value: Select(Var('index'), Var('values'), null)
},
[Var('key'), Var('value')]
)
)
)
)
)
)
)
)
)
})
Let(
{
indexRef: Index("Index_with_values"),
values: Select(
"data",
Paginate(Match(Var('indexRef')))
)
},
Call(Function("GetIndexValuesObject"), [
Var('indexRef'),
Var('values')
])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment