- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2022 08:35 AM
Hi,
I have the following JSON Array, which consists of objects and I want to remove the duplicate objects from the array. I have tried a couple of code snippets from the internet but most weren't compatible in our SN instance.
I have the following JSON array:
[
{
"slot_start": "2022-04-23 09:00:00",
"slot_end": "2022-04-23 10:30:00"
},
{
"slot_start": "2022-04-23 09:00:00",
"slot_end": "2022-04-23 10:30:00"
},
{
"slot_start": "2022-04-23 10:30:00",
"slot_end": "2022-04-23 12:00:00"
}
]
I would like to convert it to :
[
{
"slot_start": "2022-04-23 09:00:00",
"slot_end": "2022-04-23 10:30:00"
},
{
"slot_start": "2022-04-23 10:30:00",
"slot_end": "2022-04-23 12:00:00"
}
]
Any help is appreciated.
Thanks!
Solved! Go to Solution.
- Labels:
-
Script Debugger
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2022 10:33 PM
Hi Neha,
The reason is - the arrow function (=>) doesn't support in ServiceNow, please use the below code, it's tested.
var duplicateArr = [];
var finalArray = [];
arr.forEach(function (item) {
var combinedStr = item.slot_start + ',' + item.slot_end;
if (duplicateArr.indexOf(combinedStr) < 0){
duplicateArr.push(combinedStr);
finalArray.push(item);
}
});
gs.log(JSON.stringify(finalArray));
Regards,
Snehangshu Sarkar
Please mark my answer as correct if it resolves your query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2022 08:43 AM
Hi Neha,
You can refer to this Community post answer to help you.
For ease, I'll paste the code here, tweaked to your example:
var list = [
{
"slot_start": "2022-04-23 09:00:00",
"slot_end": "2022-04-23 10:30:00"
},
{
"slot_start": "2022-04-23 09:00:00",
"slot_end": "2022-04-23 10:30:00"
},
{
"slot_start": "2022-04-23 10:30:00",
"slot_end": "2022-04-23 12:00:00"
}
]
function dedupe(arr) {
return arr.reduce(function(p, c) {
// create an identifying id from the object values
var id = [c.slot_start, c.slot_end].join('|');
// if the id is not found in the temp array
// add the object to the output array
// and add the key to the temp array
if (p.temp.indexOf(id) === -1) {
p.out.push(c);
p.temp.push(id);
}
return p;
// return the deduped array
}, {
temp: [],
out: []
}).out;
}
gs.info(JSON.stringify(dedupe(list)));
The output of that code is:
[
{"slot_start":"2022-04-23 09:00:00","slot_end":"2022-04-23 10:30:00"},
{"slot_start":"2022-04-23 10:30:00","slot_end":"2022-04-23 12:00:00"}
]
Mike
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2022 08:57 PM
Hi Mike,
I have actually tried using the same method that is used in the community post answer but I am able to use this particular script in the background scripts but when I use it as a function in script includes, it is throwing me an error.
I have used it like this and I get the following error
Evaluator: com.glide.script.RhinoEcmaError: The undefined value has no properties.
The array 'conflicts' is
[
{
"slot_start": "2022-04-23 09:00:00",
"slot_end": "2022-04-23 10:30:00"
},
{
"slot_start": "2022-04-23 09:00:00",
"slot_end": "2022-04-23 10:30:00"
},
{
"slot_start": "2022-04-23 10:30:00",
"slot_end": "2022-04-23 12:00:00"
}
];
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2022 10:00 PM
Hi Neha,
If you are using it as a function in Script Include, try this:
Script Include
API Name: global.jsonUtils
var jsonUtils = Class.create();
jsonUtils.prototype = {
initialize: function() {},
dedupe: function(arr) {
return arr.reduce(this.dedupeArray, {
temp: [],
out: []
}).out;
},
dedupeArray: function(p, c) {
// create an identifying id from the object values
var id = [c.slot_start, c.slot_end].join('|');
// if the id is not found in the temp array
// add the object to the output array
// and add the key to the temp array
if (p.temp.indexOf(id) === -1) {
p.out.push(c);
p.temp.push(id);
}
return p;
// return the deduped array
},
type: 'jsonUtils'
};
Server Side Script to Call the Script Include:
var list = [
{
"slot_start": "2022-04-23 09:00:00",
"slot_end": "2022-04-23 10:30:00"
},
{
"slot_start": "2022-04-23 09:00:00",
"slot_end": "2022-04-23 10:30:00"
},
{
"slot_start": "2022-04-23 10:30:00",
"slot_end": "2022-04-23 12:00:00"
}
];
var jsonUtils = new global.jsonUtils();
gs.log(JSON.stringify(jsonUtils.dedupe(list)));
Output should be:
[{"slot_start":"2022-04-23 09:00:00","slot_end":"2022-04-23 10:30:00"},{"slot_start":"2022-04-23 10:30:00","slot_end":"2022-04-23 12:00:00"}]
Hope this helps!
Thanks,
Jenny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2022 01:14 AM
Got it, thanks!