JonasT
JonasT

Reputation: 616

remove specific line from json with jquery

I have this array:

var array = [{"a":"1","b":"2"},{"a":"3","b":"1"},{"a":"5","b":"4"}]

now i want to remove the line, lets say, where a=5. So afterwards the array looks like this:

var array = [{"a":"1","b":"2"},{"a":"3","b":"1"}]

How do i do this the easiest and fastest way?

Upvotes: 0

Views: 345

Answers (3)

Changhua
Changhua

Reputation: 1

Maybe this is your answer

array.splice(2,1);

Upvotes: 0

alexbusu
alexbusu

Reputation: 741

Javascript (non jQuery) approach: http://jsfiddle.net/VYKBc/

Upvotes: 0

Jamiec
Jamiec

Reputation: 136134

You can use jQuery.map which allows you to return null for an element to be deleted.

eg:

var array = [{"a":"1","b":"2"},{"a":"3","b":"1"},{"a":"5","b":"4"}]
var newArray = $.map(array, function(e){
   return (e.a == "5") ? null : e;
});
// newArray contains [{"a":"1","b":"2"},{"a":"3","b":"1"}]

Live example (watch the console): http://jsfiddle.net/2Yz7f/

Upvotes: 2

Related Questions