0

オブジェクトを配列にプッシュする必要があるタスクがあります。プッシュする前に、オブジェクトが既に存在するかどうかを確認する必要があるため、配列からオブジェクトを削除/削除する必要があります。サンプル コードを書きましたが、期待どおりの出力が得られません。

optionlistItemTap : function (data, index) {
    var record = data.getStore().getAt(index);
    var Id = record.raw.id;
    var arraysize = names.length;

    for (i = 0; i <= arraysize; i++) {
        if (arraysize == 0) {
            names.push(record);
            var indexId = names[i].raw.id
            var Id = record.raw.id
            break;
        }
        else if (indexId == Id) {
            names.splice(i, 1);
            break;
        }
        else
        names.push(record);
    }
},
4

2 に答える 2

1

あなたのコードを見て、私の最善の推測は、あなたがこれを必要としているということです:

optionlistItemTap : function (data, index) {
    var record = data.getStore().getAt(index); // Get the record
    var Id = record.raw.id;                    // Get the record's id
    var arraysize = names.length;              // Get the length of the `names` array

    if (arraysize == 0) {                      // If the `names` array is empty,
        names.push(record);                    // Push the record to the array.
        return;                                // Break out of the function.
    }else{                                     // Otherwise,
        for (var i = 0; i <= arraysize; i++) { // Loop through the array,
            if (names[i].raw.id == Id) {       // And if names[i]'s id matches the id of the element to add,
                names.splice(i, 1, record);    // Replace the element in `names` with the record.
                return;                        // Break out of the function.
            }
        }
    }
},
于 2013-02-01T13:08:59.577 に答える
0

indexId が else if ブロックで定義されていません

于 2013-02-01T13:02:33.800 に答える