Create object where key is variable

Creating a new Object in FQL where one of the keys is a variable (Iā€™m using ES6 here :slight_smile:):

Let({
  size: 'M',
}, {
  sizes: q.Merge({M: 3, P: 4}, {[q.Var('size')]: 2})
})

should return:

{
  sizes: {
    M: 2,
    P: 4,
  }
}

but return:

{
  sizes: {
    M: 3,
    P: 4,
    "[object Object]": 2
  }
}

How handle this situation?

Hi @mls

ToObject() do the magic here. It convert a set of 2-elements array in a document.
E.g.



client.query(
  q.ToObject([['name', 'Dennis'], ['age', 37]])
)
.then((ret) => console.log(ret))

In your case, the working query is:

 Let(
    {
        size: 'M'
    }, 
    {
        sizes: Merge({M: 3, P: 4}, 
        ToObject([[Var('size'),2]]))
   }
)

Here you can find more information.

Luigi

2 Likes

Exactly what I needed. Thank you, Luigi.