简体   繁体   中英

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.

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>"); } 

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.

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>

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"); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM