KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Count values option in .distinct() function is now supported
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: May 1, 2023

The distinct() function can be called on an entity selection or a collection.
The returned collection is automatically sorted and NULL values are not returned.

As of 4D v20, the options parameter can now accept a ck count values constant.
This ck count values constant returns the count of elements for every distinct value.
When this option is passed .distinct() returns a collection of objects containing a pair of {“value”: *value* ; “count” : *count*}

Example:

var $c1; $c2; $c3; $c4 : Collection
$c1:=New collection
$c1.push("a"; "b"; "c"; "A"; "B"; "c"; "b"; "b")
$c1.push(New object("size"; 1))
$c1.push(New object("size"; 2))
$c1.push(New object("size"; 1))

$c2:=$c1.distinct() //["a","b","c",{"size":1},{"size":2},{"size":1}]
$c3:=$c1.distinct("size") //[1,2]
$c4:=$c1.distinct("size"; ck count values)
//[{value:1,count:2},{value:2,count:1}]