我正在构建一个简单的过滤系统,我只想将一个字符串添加到一个数组中,如果已经点击链接就将其删除。我会尽力解释我能做的最好的事情。
$(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();
});
});
答案 0 :(得分:48)
$.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);
}
答案 1 :(得分:7)
我可能错了,但我相信这就像使用基本的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
};
当然,如果您真的想要简化它,可以删除一些行并将其减少为内联编码。
0 > $.inArray(newFilter,filters) ? filters.push(newFilter) : filters.splice($.inArray(newFilter,filters),1);
对于ABSOLUTE pure JS:
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);
答案 2 :(得分:4)
您可以使用lodash功能&#34; xor&#34;:
{% for error in errors %}
{% if error.propertyPath == 'myProperty' %}
<li>{{ error.message }}</li>
{% endif %}
{% endfor %}
如果你没有数组作为第二个参数,你可以简单地将变量包装成一个数组
_.xor([2, 1], [2, 3]);
// => [1, 3]
答案 3 :(得分: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();
});
});
答案 4 :(得分:0)
我发现的另一种方式:
卸下:
filters = jQuery.grep(filters, function(value) {
return value != newFilter;
});
添加:
filters.push(newFilter)
答案 5 :(得分:0)
那样的东西?
var filters = [];
// ...
var newFilter = '...';
if(-1 !== (idx = jQuery.inArray(newFilter, filters))) {
// remove
filters.splice(idx, 1);
} else {
// add
filters.push(newFilter);
}