I was looking for FQL function that does the opposite of Select
, but couldn’t find one. I just want to make sure that there aren’t any. Thanks for help
Hi @Ali-Hussein-dev - if I understand correctly, you have an object, and you want to exclude one or more of its keys but keep all of the rest?
You can do this using the Merge
function and setting the keys you want to exclude to null
.
For example, if you have the following object sample
:
{
"a": 1,
"b": 2,
"c": 3
}
and you want to exclude b
:
Merge(
Var("sample"),
{ b: null }
)
will give you:
{
"a": 1,
"c": 3
}
In a single, copy-pastable example:
Let(
{
sample: { a: 1, b: 2, c: 3 }
},
Merge(Var("sample"), { b: null })
)
Yes, you got it correctly. Thanks!
In the case of a nested object Merge
will not work as expected unless my syntax is wrong
Let(
{
sample: { a: 1, b: 2, c: { aa:10, bb:55, cc: 10 } }
},
Merge(Var("sample"), { c: { aa: null } })
)
null
here will be treated as a value, and I will lose all other fields!
{
a: 1,
b: 2,
c: {
aa: null
}
}
I checked the Merge
section to see if there is a way to handle it but nothing related is mentioned about it!
The community library faunadb-fql-lib
has a DeepMerge
UDF that you might check out. It uses the Merge function recursively.
The library is in javascript, but the underlying FQL should be applicable to anything.