0

I have an array

[0,1,2,3,4,5]

what I want to do is check for a value say 3 and then remove it

[0,1,2,4,5]

I was trying to check for it using

jQuery.inArray(questions[count], $(this).index())

but was getting quite erratic answers.

LeBlaireau
  • 14,709
  • 30
  • 95
  • 169

4 Answers4

1

grep may work for this task:

 var array = [0,1,2,3,4,5]
 var removeItem = 3;

 array = jQuery.grep(array, function(value) {
    return value != removeItem;
 });
renakre
  • 7,254
  • 3
  • 33
  • 76
1

You can use .splice:

array.splice($.inArray(3,array),1);

Working Demo

Milind Anantwar
  • 77,788
  • 22
  • 86
  • 114
1

You can use .filter() too

var array = [0,1,2,3,4,5]
var removeItem = 3;

array = array.filter(function(value) {
   return value != removeItem;
});

Example

Dhaval Marthak
  • 16,632
  • 6
  • 39
  • 66
0

Know to use of .indexOf in javascript

var arr = [0,1,2,3,4,5];
var removevalue  = 3;
arr.splice(arr.indexOf(removevalue), 1);

Fiddle

Sudharsan S
  • 14,830
  • 3
  • 27
  • 47