Estoy construyendo un sistema de filtrado simple, simplemente quiero agregar una cadena a una matriz y eliminarla si ya está allí en clic de un enlace Voy a tratar de explicar lo mejor que pueda ..Jquery: matriz simple, empujando el elemento si ya no está allí, eliminando el elemento si está allí
$(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();
});
});
¿Puedes probar 'indexof' tu cadena contra la matriz? si devuelve -1, 'push', si es mayor que -1,' pop' – MilkyWayJoe