简体   繁体   English

循环遍历整个数组,同时排除项目

[英]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. 在这种情况下,我试图为每个项目删除一个CSS类,但排除的项目除外。

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; 我也尝试过类似if(array [i] == 3 ||(i-1)= 2){继续; 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 使用continue

In the following example we want to exclude "item2" and fixed variable naming/reference : 在以下示例中,我们要排除“ item2” 和固定变量的命名/引用

 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? 数组项目是元素的ID? - if so you need the id indicator (" # ") in the code -如果需要,则代码中需要id指示符(“ # ”)

$('#' + 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 编辑-我刚刚注意到您的数组称为“数组”,但是在else块中,您将其称为项目

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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