Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - Simple Array, pushing item in if its not there already, removing item if it is there

Tags:

I'm building a simple filtering system, I simply want to add a string to an array and remove it if its already there on click of a link. I'll try to explain the best I can..

$(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();     }); }); 
like image 488
Iamsamstimpson Avatar asked Mar 07 '12 15:03

Iamsamstimpson


People also ask

How to remove last item in array JavaScript?

prototype. pop() The pop() method removes the last element from an array and returns that element.

How to pop an element from array JavaScript?

JavaScript Array pop()The pop() method removes (pops) the last element of an array. The pop() method changes the original array. The pop() method returns the removed element.

What is the opposite of array push?

pop(): We use pop JavaScript to remove the last element in an array. Moreover, this function returns the removed element. At the same, it will reduce the length of the array by one. This function is the opposite of the JavaScript array push function.

Is array push ES6?

ES6 - Array Method push()push() method appends the given element(s) in the last of the array and returns the length of the new array.


2 Answers

$.inArray() returns the index of the item if it is found, and -1 otherwise (just like indexOf() does, when supported). Therefore, you can write something like:

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); } 
like image 100
Frédéric Hamidi Avatar answered Oct 15 '22 15:10

Frédéric Hamidi


I could be wrong, but I believe this is as simple as using basic 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 }; 

Of course if you really want to simplify it you could remove some lines and reduce it to inline coding.

0 > $.inArray(newFilter,filters) ? filters.push(newFilter) : filters.splice($.inArray(newFilter,filters),1); 

For ABSOLUTE pure JS:

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

Broken down:

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); 
like image 28
SpYk3HH Avatar answered Oct 15 '22 15:10

SpYk3HH