Skip to content

Instantly share code, notes, and snippets.

@PierBover
Last active Aug 6, 2021
Embed
What would you like to do?
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