Last active
August 6, 2021 19:11
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')] | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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