how to remove one sysid from array of sysid

Debarpita
Tera Contributor

Hi All,

please help me how to remove sysid from array?

var arr=("5dbe64308782c5d0d1f7dbd83cbb3547","5dbe64308782c5d0d1f7dbd83cbb3547","88f93fa5db9930944d28264cd39619ca","88f93fa5db9930944d28264cd39619ca","154e8466dbf4401494d1983cca961956","154e8466dbf4401494d1983cca961956","41ba31a9475ec9d4d2dec071e36d43fe","41ba31a9475ec9d4d2dec071e36d43fe","af88bc7c872ef450021584490cbb35c2");
var previous_assignedto = "5dbe64308782c5d0d1f7dbd83cbb3547";


for( var i = 0; i < arr.length; i++){
if ( arr[i] != previous_assignedto) {
arr.splice(i, 1);
}
}

gs.print(arr);

result is not correct

arr:- 

af88bc7c872ef450021584490cbb35c2

I want to remove previous_assignedto value from arr array. Kindly help me 

 

 

1 ACCEPTED SOLUTION

Shivani M S
Kilo Guru

var arr=["5dbe64308782c5d0d1f7dbd83cbb3547","5dbe64308782c5d0d1f7dbd83cbb3547","88f93fa5db9930944d28264cd39619ca","88f93fa5db9930944d28264cd39619ca","154e8466dbf4401494d1983cca961956","154e8466dbf4401494d1983cca961956","41ba31a9475ec9d4d2dec071e36d43fe","41ba31a9475ec9d4d2dec071e36d43fe","af88bc7c872ef450021584490cbb35c2"];
var previous_assignedto = "5dbe64308782c5d0d1f7dbd83cbb3547";

var val=[];
for( var i = 0; i < arr.length; i++){
if ( arr[i] != previous_assignedto) {
val.push(arr[i]);
}
}

gs.print(val);

 

 


 

View solution in original post

6 REPLIES 6

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Debarpita 

 

Please try below code:

 

var arr = ["5dbe64308782c5d0d1f7dbd83cbb3547", "5dbe64308782c5d0d1f7dbd83cbb3580", "88f93fa5db9930944d28264cd39619ca", "88f93fa5db9930944d28264cd39619ca", "154e8466dbf4401494d1983cca961956", "154e8466dbf4401494d1983cca961956", "41ba31a9475ec9d4d2dec071e36d43fe","41ba31a9475ec9d4d2dec071e36d43fe","af88bc7c872ef450021584490cbb35c2"];
// Array is enclosed in Square Brackets []

var previous_assignedto = "5dbe64308782c5d0d1f7dbd83cbb3580"; // changes value of sys_id just to test it

var index = arr.indexOf(previous_assignedto);

var x = arr.splice(index, 1);

gs.print(index);
gs.print(x);

gs.print(arr);

 

This may not be exact requirement but you can refer this concept and use in your requirement accordingly. I have changed sys_id assuming there would be single sys_id only which you would want to remove.

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

in code i have written this:

gs.log("Ruleuuuy-1-Testing Grp Assign---" + asgUserList);

gs.log("Ruleuuuy-2-previous_assignedto-" + previous_assignedto);
var leastBusyCount = 0;
var tempCount = 0;


var index = asgUserList.indexOf(previous_assignedto);

var x = asgUserList.splice(index, 1);
gs.log("Ruleuuuy-3-index "+ index);
gs.log("Ruleuuuy-4-x "+ x);

 

in logs i am getting this:

Ruleuuuy-1-Testing Grp Assign---5dbe64308782c5d0d1f7dbd83cbb3547,5dbe64308782c5d0d1f7dbd83cbb3547,88f93fa5db9930944d28264cd39619ca,88f93fa5db9930944d28264cd39619ca,154e8466dbf4401494d1983cca961956,154e8466dbf4401494d1983cca961956,41ba31a9475ec9d4d2dec071e36d43fe,41ba31a9475ec9d4d2dec071e36d43fe,af88bc7c872ef450021584490cbb35c2

Ruleuuuy-2-previous_assignedto-5dbe64308782c5d0d1f7dbd83cbb3547

Ruleuuuy-3-index -1

 

Ruleuuuy-4-x af88bc7c872ef450021584490cbb35c2

 

In background script i am getting correct output , it is working fine

 

 

  

Hi @Debarpita 

 

So the solution provided by me is working as expected, right?

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

No it is not working for me. thanks for the help