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

Kevin
  • 1,830
  • 3
  • 23
  • 37

4 Answers4

1

You could use jQuery inArray() to find the item, then remove it

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

You may even be able to use jQuery grep to find the item and splice it out:

How to remove specifc value from array using jQuery

Community
  • 1
  • 1
Ethan Hayon
  • 326
  • 1
  • 9
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().

thecodeparadox
  • 81,835
  • 21
  • 131
  • 160
0

You could change the questionCollection to be a hash of arrays. Or you could build and indexing hash from it first, assuming you only need to do that once to remove multiple questions.

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);
    }
}
dlamblin
  • 40,676
  • 19
  • 92
  • 127
0

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

function RemoveQuestion(question) 
{ 
    questionCollection = $.grep(questionCollection, function (item) {
        return (item.PprID === question.PprID);
    }, true);
}