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 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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().

Comments

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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.