1

I have a javascript object array like

var objArr = [Object{key="1", value="a"}, Object{key="2", value="b"}, ...]

Do we have any Jquery method find the object and delete it. I know using $.each

$.each(objArr, function(index, obj) {

})

But do we have any easy and efficient solution for this?

Zbigniew
  • 25,495
  • 6
  • 53
  • 63
Lolly
  • 28,770
  • 36
  • 103
  • 140
  • 2
    it's important to note that every solution to this problem will always be linear efficiency. `each` just loops, `filter` just loops. nothing magic. – jbabey Sep 06 '12 at 15:12

2 Answers2

3

Without jQuery, by simply using the filter function of javascript :

var filtered = objArr.filter(function(o){return o.key!='badkey';});

(note that the MDN page offers tips for the compatibility with very old browsers)

Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
0

To find the element you could use the .grep() function Example:Filter an array of numbers to include numbers that are not bigger than zero.

$.grep( [0,1,2], function(n,i){
return n > 0; 
},true);

Result: [0]

Or if you only need to find the position of that element, you could use the .inArray() function Example: Find the position of the element that matches with: "1"

var arr = [ 4, 2, 3, 1, "hello" ];
var exist = $.inArray(1, arr);

Result:

exist = 3

And for delete, theres a post that maybe can resolve your problem How to remove specifc value from array using jQuery

Community
  • 1
  • 1
Lio
  • 5
  • 1
  • 3
  • 5