使用jQuery inArray使用JavaScript Object数组 [英] Using jQuery inArray with array of JavaScript Objects

查看:117
本文介绍了使用jQuery inArray使用JavaScript Object数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JavaScript的数组对象的工作这样:

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"}];

我的最终努力尝试当我知道对象的ID从数组中删除的对象。我试图用code,它是这样的:

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);
});

因此​​,这表明,我试图使用jQuery的grep按指数(I),其中我试图通过使用jQuery inArray找回删除的元素。当然,上面的code将无法工作,因为2应该对应于数组中的一个项目,这是所有的JavaScript对象(物体永远不会等于2)。我需要的是这样的:

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);

有没有人使用inArray获取JavaScript对象的索引,使用该对象中的一个字段值过任何成功呢?任何帮助将是AP preciated。谢谢你。

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";
});

我想我在想太深,当溶液真是pretty容易。我只是想从一个数组中删除一个JavaScript对象,只要我知道一个特定的属性在该对象的价值。以上解决方案使用jQuery的grep来回报一切从阵列除了其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 是列表项,那么这样的事情应该做的工作:

n is your list item, so something like this should do the job:

$.grep(issuesArray, function(n) { return n.ID != "2"; })

这篇关于使用jQuery inArray使用JavaScript Object数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆