UDFs with Arrays

Having some issues with writing functionality into a UDF.

Your documentation under Sets - reduce() shows that the following will count the number of items using reduce.

Product.all().take(9).reduce((s, v) => s + 1) - this does not work with my Product collection. There are some other reduce functions on the reduce() page that dont work either.

What I am trying to do is to find the maximum value for a specific object field (orderid:) in the Order collection and then pick the value that is one higher. This will allow me to create new records with an identifier one higher than the max. I want to add this to an UDF that will create orders.

Every method I try does not work because the core problem is that I cant find a way to strip out just the values from an array of objects returned when I run Order.all(). I get back an array of objects that does not work with FQL functions.

[{orderid: 101}, {orderid: 102}, {orderid: 104}]

The functions in FQL language, such as reduce, only run on simple arrays of values, such as [1,2,3,4] – they dont run on an array of objects. I also tried to use the Math.max function, but it also only runs on simple arrays, not arrays of objects.

I also tried to use the Object.values function to strip the values out of the objects, but this works with Product.byID(), but not with Product.all(). Once again, I think this does not work on an array of objects.

What key concept am I missing here that will allow me to apply FQL language functions to arrays of objects that are returned when I query a set from a collection? I’m sure that one simple example would help to understand this.

BTW, love the power of UDFs in Fauna.

This will get you the object with the greatest orderid

[{orderid: 101}, {orderid: 102}, {orderid: 104}].reduce((a, b)=>{
  if(a.orderid > b.orderid){
  } else{

That code will return:

{orderid: 104}