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

Please remember to mark response helpful.

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