2

これが私が現在検索している方法です:

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

すべての配列インスタンスをループしているような気がしますが、その中の配列を見るのは少し遅いかもしれません。

どんな助けでも大歓迎です。

ありがとう

ケビン

4

4 に答える 4

1

jQuery inArray()を使用してアイテムを検索し、削除することができます

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

jQuery grepを使用してアイテムを検索し、スプライスすることもできます。

jQueryを使用して配列から特定の値を削除する方法

于 2012-08-08T19:02:45.277 に答える
0

配列を確認するには、次のものが必要です。

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

配列内の要素を見つけるには、jQueryを使用できます.inArray()

于 2012-08-08T18:59:32.197 に答える
0

questionCollectionを配列のハッシュに変更できます。または、複数の質問を削除するために1回だけ実行する必要があると仮定して、最初にハッシュを作成してインデックスを作成することもできます。

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 に答える