- 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 11:34 AM
Hi Neha,
See if this helps you. I tried it in https://replit.com/
var a =[
{
"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 duplicateArr = [];
var finalArray = [];
a.forEach((item) => {
var combinedStr = item.slot_start + ',' + item.slot_end;
if (duplicateArr.indexOf(combinedStr) < 0){
duplicateArr.push(combinedStr);
finalArray.push(item);
}
});
console.log(finalArray);
output 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'
}
]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2022 09:00 PM
Hi Ankit,
I think this particular syntax is not supported by our ServiceNow JS browser. I get the following error
Evaluator: com.glide.script.RhinoEcmaError: syntax error
script : Line(100) column(20)
a.forEach((item) => {
Thanks!
- 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-23-2022 01:14 AM
Thanks, it is working!