remove duplicates from array

gtk
Mega Sage

on scoped application form we have requested for, opened by and watchlist

1. trying to push all these values into any array 'a1'

2. tries using var au =  gloabl.ArrayUtils();

au.unique(a1);

not getting result from business rule and the same is applied on Background script , in BG it is working but not in BR

var currentWL = "9171f4e087f50d9471760dcd8bbb3506,9171f4e087f50d9471760dcd8bbb3506,d69e968b4796015011097601e36d4303,0c8f67761b035810acb7b887cc4bcb0d";



var updatedWL = currentWL ;
gs.info ("Updated Watch List is " + updatedWL);

var emailArray = updatedWL.split(',');

var arrayUtil = new global.ArrayUtil();
//with duplicates
gs.info("final array = "+emailArray);

var finalArray = arrayUtil.unique(emailArray);
//without duplicates
gs.info("result array = "+finalArray.toString());

 

any help appreciated

#scope

1 ACCEPTED SOLUTION

If you are just trying to delete the duplicate items tried something like this:

var a1=[] // Store the data localy, original and duplicates
 a1.push(current.caller); // send caller to array
var watchlist = current.watchlist.toString();
 a1.push(watchlist);//send wathlist to array
 a1.push(current.opened_by); // send opened by to array
var au = []; // new array

//Delete the duplicated items with 
for (var i = 0; i < a1.length; i++) {
// In the iff condition the indexOf return -1 if not exist that element in the array and insert in the au array
  if (au.indexOf(a1[i]) === -1) {
    au.push(a1[i]);
  }
}
gs.inof(a1); //a1 contain original and duplicated values
gs.inof(au); //au contain not duplicated values

Or tried : 

var a1=[] // Store the data localy, original and duplicates
 a1.push(current.caller); // send caller to array
var watchlist = current.watchlist.toString();
 a1.push(watchlist);//send wathlist to array
 a1.push(current.opened_by); // send opened by to array
var au = a1.reduce(function (accumulator, currentValue) {
  if (accumulator.indexOf(currentValue) === -1) {
    accumulator.push(currentValue);
  }
  return accumulator;
}, []);


gs.inof(a1); //a1 contain original and duplicated values
gs.inof(au); //au contain not duplicated values

 

I hope that cant help you, if you dont understand just look for delete duplicate items in array in the internet.

 

View solution in original post

3 REPLIES 3

Marco0o1
Tera Sage

Hi @gtk ,

 

Can you give more context about your problem, and maybe all your code (you say, you are trying to push the variable in au, but i cant see in the code the variable). That looks the problem is in the logic insted of servicenow apis.

@Marco0o1 , thanks for your interest

var a1 = []; // new array

a1.push(current.caller); // send caller to array

var watchlist = current.watchlist.toString();

a1.push(watchlist);//send wathlist to array

a1.push(current.opened_by); // send opened by to array

gs.inof(a1);

now if caller is also available in watchlist, then remove duplicates

//use arraytuils script include to remove duplicates from array

var au = new global.Arrayutil();

var autil = au.unique(a1);

gs.info(autil);

// here I am not able to remove duplicates from array

If you are just trying to delete the duplicate items tried something like this:

var a1=[] // Store the data localy, original and duplicates
 a1.push(current.caller); // send caller to array
var watchlist = current.watchlist.toString();
 a1.push(watchlist);//send wathlist to array
 a1.push(current.opened_by); // send opened by to array
var au = []; // new array

//Delete the duplicated items with 
for (var i = 0; i < a1.length; i++) {
// In the iff condition the indexOf return -1 if not exist that element in the array and insert in the au array
  if (au.indexOf(a1[i]) === -1) {
    au.push(a1[i]);
  }
}
gs.inof(a1); //a1 contain original and duplicated values
gs.inof(au); //au contain not duplicated values

Or tried : 

var a1=[] // Store the data localy, original and duplicates
 a1.push(current.caller); // send caller to array
var watchlist = current.watchlist.toString();
 a1.push(watchlist);//send wathlist to array
 a1.push(current.opened_by); // send opened by to array
var au = a1.reduce(function (accumulator, currentValue) {
  if (accumulator.indexOf(currentValue) === -1) {
    accumulator.push(currentValue);
  }
  return accumulator;
}, []);


gs.inof(a1); //a1 contain original and duplicated values
gs.inof(au); //au contain not duplicated values

 

I hope that cant help you, if you dont understand just look for delete duplicate items in array in the internet.