pfbarnet
pfbarnet

Reputation: 111

Loop through entire array while excluding an item

I have an array of items that I'd like to loop through and apply some code to, excluding one item (the clicked-on item). I've tried using splice but I don't want to remove the array item, just skip over it. In this case, I'm trying to remove a CSS class for each item, except the excluded one.

I've tried a few methods, using splice for one which isn't what I need. I've also tried something like if (array[i] == 3 || (i - 1) = 2) { continue; else { .... } but can't seem to get that to work.

   var array = ["item1", "item2", "item3"];
   var i;
   for (i = 0; i < items.length; i++) {
       if(array[i] is the excluded one){
               skip over}
    else { $(items[i]).removeClass('class');
    }

No error messages, just not working as expected.

Upvotes: 0

Views: 1963

Answers (4)

Markus Zeller
Markus Zeller

Reputation: 9090

Elegant way in functional programming:

const array = ["item1", "item2", "item3"];

// item to ignore
const ignore = "item2";

// use filter to keep only items which are not ignored
const newarray = array.filter((e)=> { return e !== ignore } );

// iterate over remaining
newarray.forEach((e)=> { $(e).removeClass("class"); });

Upvotes: 0

gavgrif
gavgrif

Reputation: 15499

are the array items the id of the elements? - if so you need the id indicator ("#") in the code

$('#' + items[i]).removeClass('class')

EDIT - i just noticed that your array is called"array", but in the else block you refer to it as items

   var items= ["item1", "item2", "item3"];
   var i;
   for (i = 0; i < items.length; i++) {
       if(items[i] is the excluded one){
               skip over}
    else { $('#' + items[i]).removeClass('class');
    }

I still think that there is a better way to do this - but try making the names the same and see if that works.

Upvotes: 0

seantunwin
seantunwin

Reputation: 1768

Use continue

In the following example we want to exclude "item2" and fixed variable naming/reference:

var items = ["item1", "item2", "item3"];
var i;
for (i = 0; i < items.length; i++) {
  if(items[i] === "item2"){
    continue;
  }

   //$(items[i]).removeClass('class');
   document.write(items[i] + "<br>");
}

Upvotes: 1

Anton Christensen
Anton Christensen

Reputation: 36

I think something like this is what you need:

<script type="text/javascript">
    $(document).ready(function(){
        $(".click").click(function(){ //When item with class click is clicked
               var items = ["item1", "item2", "item3"];
               var i;
               var itemid = this.id;
               for (i = 0; i < items.length; i++) { //go through array
                    if(items[i]==itemid){ //check if array item is the same as id of the class click
                        // Don't do anything, this is the one you clicked
                    }else{
                        $("#"+items[i]).removeClass('removeme'); // Remove class of all other with class click
                    }
               }
        });
    });
</script>
<div class="item1 click removeme" id="item1">Test1</div>
<div class="item2 click removeme" id="item2">Test2</div>
<div class="item3 click removeme" id="item3">Test3</div>

Upvotes: 0

Related Questions