Removing duplicate objects from a JSON aray

Neha Pendem
ServiceNow Employee
ServiceNow Employee

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!

1 ACCEPTED SOLUTION

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.

View solution in original post

8 REPLIES 8

Ankit Agrawal
Kilo Contributor

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'
  }
]

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!

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.

Thanks, it is working!