2

This is how I am currently searching:

function RemoveQuestion(question)
    {
        $.each(questionCollection, function (index, item)
        {
            var indexToRemove;
            if (item["PprID"] == question["PprID"])
            {
                //This question needs to be removed.
                indexToRemove = index;
                return false;
            }
        });

        questionCollection.splice(indexToRemove, 1);
    }

I feel like looping through every array instance, then looking at the array inside of it might be a bit slow.

Any help is appreciated.

Thanks

Kevin

4

4 回答 4

1

您可以使用 jQuery inArray() 查找项目,然后将其删除

http://api.jquery.com/jQuery.inArray/

您甚至可以使用 jQuery grep 来查找项目并将其拼接出来:

如何使用jQuery从数组中删除特定值

于 2012-08-08T19:02:45.277 回答
0

To check the array you need this:

if( Object.prototype.toString.call( item ) === '[object Array]' ) {
  // do some
}

To find element within an array you can use jQuery .inArray().

于 2012-08-08T18:59:32.197 回答
0

您可以将 更改questionCollection为数组的散列。或者,您可以先从中构建和索引哈希,假设您只需要这样做一次即可删除多个问题。

function IndexQuestions()
{
    $.each(questionCollection, function (index, item)
    {
        questionIndex[item["PprID"]] = index;
    });
}
function RemoveQuestion(question)
{
    var indexToRemove = questionIndex[question["PprID"]];
    if (indexToRemove != null)
    {
        questionCollection.splice(indexToRemove, 1);
    }
}
于 2012-08-08T18:59:45.690 回答
0

http://api.jquery.com/jQuery.grep/

function RemoveQuestion(question) 
{ 
    questionCollection = $.grep(questionCollection, function (item) {
        return (item.PprID === question.PprID);
    }, true);
}
于 2012-08-08T19:11:29.320 回答