-4

I have an array in javascript. I need to remove an item from it. I have to iterate over the array and check whether there is a value called 'mastercheck'. If the value is there in array, I have to remove it and get the remaining items. How to do?

Typically my array consists of value like mastercheck,60154,60155....

Sanjay Malhotra
  • 147
  • 2
  • 5
  • 11

3 Answers3

4

First use the indexOf method to determine the index of the item with the needed value. Then you can use the splice method to remove the item at found index.

Something like that:

var array = ['mastercheck', '60154', '60155'];
var index = array.indexOf('mastercheck'); // get the index
array.splice(index, 1); // remove the item
ᗩИᎠЯƎᗩ
  • 2,076
  • 5
  • 26
  • 39
Valentin S.
  • 494
  • 2
  • 9
2
var arr = ['mastercheck',60154,60155];

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

console.log(arr);
Misha
  • 127
  • 7
1

Use this code jsFiddle

var arr = ['mastercheck', '60154', '60155'];
var index = arr.indexOf('mastercheck');
arr.splice(index, 1);
Martin
  • 2,076
  • 1
  • 19
  • 39