summaryrefslogtreecommitdiff
path: root/content/posts/get-array-of-unique-objects-in-servicenow.md
blob: c85692bde9999fab0f16eebc69c5e7163ab7136b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---
title: "Get Array of Unique Objects in Servicenow"
description: "How to get only unique objects from an array of objects in ServiceNow"
date: 2024-06-14T12:29:18+02:00
tags: ['servicenow', 'javascript']
---

There's no secret that the Javascript support (serverside) in ServiecNow is lacking. And today I came across another little quirk. In ServiceNow we have the `ArrayUtil.unique()` to get an Array of unique values, but this does not support objects. Neither does ServiceNow support `Map()` or `Set()` on the serverside, so here's a little snippet to filter an array of objects and receive unique objects based on a object `key`.

<!--more-->

```javascript
/**
 * Get unique objects from an array of object, by key.
 *
 * @param {Array.<Object>} arr - The array of objects
 * @param {string} key - The object key that must be unique
 * @returns {Array.<Object>} An array of unique objects
 */
function uniqueObjects(arr, key) {
    return arr.filter(function(value, index, self) {
        return self.map(function(x) {
            return x[key];
        }).indexOf(value[key]) == index;
    });
}
```

Here's a example from a fresh developer instance.

```javascript
var managers = [];
var gr = new GlideRecord('sys_user');
gr.addActiveQuery();
gr.addNotNullQuery('manager');
gr.query();

while(gr.next()) {
    var managerId = gr.getValue('manager');
    var managerUsername = gr.manager.user_name.getValue();
    managers.push({
        userId: managerId,
        username: managerUsername,
    });
}

var uniqueManagers = uniqueObjects(managers, 'userId');

gs.print(managers.length);
gs.print(uniqueManagers.length);

// ---------- OUTPUT ----------
// *** Script: 13
// *** Script: 11
```