How to remove the specific value from Array Dynamically

HR Rajashekar
Mega Expert

Hi All,

I have requirement in scripting part where in

I created array and using "push" method to insert the value.

But I wanted to remove specific value from array and it will be selected dynamically.

Could any one help me out on this.

 

 

1 ACCEPTED SOLUTION

Matt Taylor - G
Giga Guru

You might try the splice function to drop a particular value from the array. Check the info about the splice function here.

 

// Given the following array:
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

// Iterate through the array and test each value until we find the 
// one we're looking for and remove it
for( var i = 0; i < arr.length; i++){ 
   if ( arr[i] === 5) {
     arr.splice(i, 1); 
   }
}

//=> [1, 2, 3, 4, 6, 7, 8, 9, 0] This is the value of arr after the splice

View solution in original post

10 REPLIES 10

Hi Sanket,

 

Thank you for your response.

Still i am not finding the exact the solution.

My requirement is, I want to delete the selected value either first/last/own choice for only once.

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Rajashekar,

So you have a array and now you want to remove specific value from that array.

Is that array unique?

if not then if multiple occurrences of that value are present then all are to be removed?

script below for only 1 occurrence

var array = [2,5,9];

gs.info('Initial Array is: ' + array);

var valueToBeRemoved = 5;

var index = array.indexOf(valueToBeRemoved);

if (index > -1) {
array.splice(index, 1);
}

gs.info('Final Array is: ' + array);

script below for removing all occurrence of that value

var array = [2,5,9,5];

gs.info('Initial Array is: ' + array);

var valueToBeRemoved = 5;

var finalArray = removeItem(array,valueToBeRemoved);

gs.info('FinalArray is: ' + finalArray);

function removeItem(array, item) {
    for(i = 0; i<array.length; i++){
        if(array[i] == item) {
            array.splice(array.indexOf(item), 1);
            i--;
        }
    }
return array;

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

Thank you for your response.

Thanks for help. I had similar requirement and it worked as expected.

getEscalationGroupfromServices: function(current) {
var service = current.getValue('u_service');
var assigngrp = current.getValue('u_assignment_group');
var serArrUniqueVal;
var serArrVal = [];
var grpVal = new GlideRecord("sys_user_group");
grpVal.addQuery('u_service', service); 
grpVal.addEncodedQuery("u_serviceISNOTEMPTY");
grpVal.query();
while (grpVal.next()) {
serArrVal.push(grpVal.sys_id.toString());

for (var i = 0; i < serArrVal.length; i++) {
if (serArrVal[i] == assigngrp) {
serArrVal.splice(serArrVal.indexOf(assigngrp), 1);
i--;
}
}
}
serArrUniqueVal = new global.ArrayUtil().unique(serArrVal); //get unique value
return 'sys_idIN' + serArrUniqueVal;
},

Monica Deshpand
Giga Contributor

Thanks for help. I had similar requirement and it worked as expected as per suggestions above!!

getEscalationGroupfromServices: function(current) {
var service = current.getValue('u_service');
var assigngrp = current.getValue('u_assignment_group');
var serArrUniqueVal;
var serArrVal = [];
var grpVal = new GlideRecord("sys_user_group");
grpVal.addQuery('u_service', service); 
grpVal.addEncodedQuery("u_serviceISNOTEMPTY");
grpVal.query();
while (grpVal.next()) {
serArrVal.push(grpVal.sys_id.toString());

for (var i = 0; i < serArrVal.length; i++) {
if (serArrVal[i] == assigngrp) {
serArrVal.splice(serArrVal.indexOf(assigngrp), 1);
i--;
}
}
}
serArrUniqueVal = new global.ArrayUtil().unique(serArrVal); //get unique value
return 'sys_idIN' + serArrUniqueVal;
},