I'm working with an array of JavaScript Objects as such:
var IssuesArray = [{"ID" : "1", "Name" : "Issue1"},
{"ID" : "2", "Name" : "Issue2"},
{"ID" : "3", "Name" : "Issue3"}];
My end effort is trying to remove an object from the array when I know the ID of the object. I'm trying to use code that is something like this:
$.grep(IssuesArray, function(n, i) {
return i != $.inArray("2", IssuesArray);
});
So this shows that I'm trying to use jQuery grep to remove an element by index (i), which I am trying to retrieve by using jQuery inArray. Of course the code above will not work because "2" should correspond to an item in the array, which are all JavaScript objects (an object will never equal "2"). I need something like:
$.inArray(javascriptObject.Name=="2", IssuesArray);
Has anyone ever had any success using inArray to get indexes of JavaScript objects, using a field value within that object? Any help would be appreciated. Thanks.
UPDATE/CLARIFICATION: A couple people have been confused by my question, but I received an answer that works nonetheless. I'm using:
IssuesArray = $.grep(IssuesArray, function(n) {
return n.ID != "2";
});
I think I was thinking about it too deep, when the solution was really pretty easy. I simply wanted to remove a JavaScript object from an array, so long as I knew a particular property's value in that object. The above solution uses jQuery's grep to return everything from the array except any object whose ID == "2". As usual, thanks for the quick answers. A couple answers were good solutions and would have worked using (using "splice", for example), but this solution seems to be the shortest most straightforward. Thanks again.
n
is your list item, so something like this should do the job:Without using jQuery or other frameworks:
Not sure if I understood your question correctly, but I would do:
Simplify??
Why a list of hashes when a single hash will do?