2012-03-07 6 views
15

単純なフィルタリングシステムを構築していますが、配列に文字列を追加して、リンクをクリックします。私は私が間違っている可能性Jquery - 単純な配列で、アイテムがまだ存在しない場合は、そこにあるアイテムを削除します。

$(document).ready(function(){ 
    //so I start with an empty array 
    var filters []; 
    //when a link is clicked I want to add it to the array.. 
    $('li a', context).click(function(e){ 
     //so I get the value held in the data-event attribute of the clicked item example: "john" 
     newFilter = $(this).attr('data-event'); 
     //this is where I get stuck, I want to test to see if the string I now have 
     //in 'newFilter' is in the array already or not.. if it is in the array I 
     //want to remove it, but if it doesnt exist in the array i want to add it.. 
     if(jQuery.inArray(newFilter, filters){ 
      //add to array 
     } else { 
      //remove from array 
     }; 
    e.preventDefault(); 
    }); 
}); 
+3

あなたの文字列を配列に対して 'indexof'できますか? -1を返すと 'push'、-1より大きければ' pop' – MilkyWayJoe

答えて

41

$.inArray()は、それが見つかった場合アイテムのインデックスを返し、そうでなければ-1(ちょうどのようなindexOf()は、サポートされている場合)。

var found = jQuery.inArray(newFilter, filters); 
if (found >= 0) { 
    // Element was found, remove it. 
    filters.splice(found, 1); 
} else { 
    // Element was not found, add it. 
    filters.push(newFilter); 
} 
6

..私ができる最善を説明しようとするでしょうが、私は、これは基本的なJavaScriptを使用してのと同じくらい簡単であると考えている:もちろん[.push.splice]

if($.inArray(newFilter, filters)<0) { 
    //add to array 
    filters.push(newFilter); // <- basic JS see Array.push 
} 
else { 
    //remove from array 
    filters.splice($.inArray(newFilter, filters),1); // <- basic JS see Array.splice 
}; 

本当に単純化したいのであれば、いくつかの行を削除し、それをインラインコーディングに減らすことができます。

ABSOLUTE純粋JSについて
0 > $.inArray(newFilter,filters) ? filters.push(newFilter) : filters.splice($.inArray(newFilter,filters),1); 

:ダウンブロークン

var i; (i=filters.indexOf(newFilter))<0?filters.push(newFilter):filters.splice(i,1); 

に:

var i; // Basic variable to be used if index of item exist 
// The following is simply an opening to an inline if statement. 
// It's wrapped in() because we want `i` to equal the index of the item, if found, not what's to follow the `?`. 
// So this says "If i = an index value less than 0". 
(i=filters.indexOf(newFilter)) < 0 ? 
    // If it was not found, the index will be -1, thus push new item onto array 
    filters.push(newFilter) : 
     // If found, i will be the index of the item found, so we can now use it to simply splice that item from the array. 
     filters.splice(i,1); 
+4

'$ .inArray()'はjQuery関数です。それは "基本的なJavaScript"ではありません... – aendrew

+0

@aendrew私は方法のiinsideを参照していた – SpYk3HH

0

私が見つけたもう一つの方法:したがって、あなたのような何かを書くことができます

削除:

filters = jQuery.grep(filters, function(value) { 
    return value != newFilter; 
}); 

追加:

filters.push(newFilter) 
1

あなたは特別な理由がない限り、配列を使うためには、代わりにオブジェクトを使うことをお勧めします。

$(document).ready(function(){ 
    //so I start with an empty array 
    var filters {}; 
    //when a link is clicked I want to add it to the array.. 
    $('li a', context).click(function(e){ 
     //so I get the value held in the data-event attribute of the clicked item example: "john" 
     newFilter = $(this).attr('data-event'); 
     //this is where I get stuck, I want to test to see if the string I now have 
     //in 'newFilter' is in the array already or not.. if it is in the array I 
     //want to remove it, but if it doesnt exist in the array i want to add it.. 
     if (filters.hasOwnProperty(newFilter)) { 
      // remove from object 
      delete filters[newFilter]; 
     } else { 
      //add to object 
      filters[newFilter] = 'FOO'; // some sentinel since we don't care about value 
     }; 
    e.preventDefault(); 
    }); 
}); 
+0

ちょっとjbabey ..どのようにconsole.logを使用してオブジェクトの内容を表示する初心者の質問?現在私は取得 - [オブジェクトオブジェクト] – Iamsamstimpson

+1

console.log(myObject.propName)またはconsole.log(myObject ['propName']) – jbabey

0

このようなものはありますか?

var filters = []; 
// ... 
var newFilter = '...'; 
if(-1 !== (idx = jQuery.inArray(newFilter, filters))) { 
    // remove 
    filters.splice(idx, 1); 
} else { 
    // add 
    filters.push(newFilter); 
} 
1

あなたは「XOR」lodash機能を使用することができます。

_.xor([2, 1], [2, 3]); 
// => [1, 3] 

あなたは第二パラメータとして配列を持っていない場合があるSimpy配列

var variableToInsertOrRemove = 2; 
_.xor([2, 1], [variableToInsertOrRemove]); 
// => [1] 
_.xor([1, 3], [variableToInsertOrRemove]); 
// => [1, 2, 3] 
に変数をラップすることができます

ここに文書があります:https://lodash.com/docs/4.16.4#xor

関連する問題