簡體   English   中英

從數組中刪除多個元素

[英]Remove multiple elements from array

我想在數組顯示之前從數組中刪除多個特定元素。 這是我的代碼,但是它沒有顯示任何元素:

$('[data-fancybox]').on('click', function() {
  var visibleLinks = $('.fancybox:visible');

  $.fancybox.open( visibleLinks, {
      //options go here
caption: function (instance, item) {
        var caption, link, collectTags, tags, filterTags, filteredTags;

        function format(tpl, binding) {
            if (typeof binding != 'function') return format(tpl, function (_, name) {
                return binding[name];
            });
            return tpl.replace(/\$(\w+)/g, binding);
        }

        caption = $(this).data('caption');
        link = format('<br>See more pictures', item);
        collectTags = $(this).parent().attr("class").split(' ');
        function createTag(it) {
            return format("<a href='$site$it'>$it</a>", {
                site: (it == 'wedding' || it == 'concert') ? 'http://example.com/gallery/#filter=.' : 'http://example.com/gallery/#filter=.',
                it: it
            });

        }
        filterTags = ['churchevent', 'corporate'];
        filteredTags = tags.filter(function(itm){return itm!== filterTags});

        tags = $.map(collectTags, createTag);
        return  [].concat(caption ? [caption, link] : link).concat(filteredTags.slice(1).join(', ')).join('<br>');
    }
  }, visibleLinks.index( this ) );

  return false;   
});

我猜想是因為您編寫了“刪除多個特定元素”,所以要刪除filterTags。

如果是這種情況,請更改此設置:

filterTags = ['churchevent', 'corporate'];
filteredTags = tags.filter(function(itm){return itm!== filterTags});

tags = $.map(collectTags, createTag);
return  [].concat(caption ? [caption, link] : link).concat(filteredTags.slice(1).join(', ')).join('<br>');

對此:

filterTags = ['churchevent', 'corporate'];

tags = $.map(collectTags, createTag);
filteredTags = tags.filter((item)=>{
    for(tag in filterTags)  if (item.indexOf(filterTags[tag]) != -1) return false;
    return true;
});

return  [].concat(caption ? [caption, link] : link).concat(filteredTags.slice(1).join(', ')).join('<br>');

否則只需在過濾方法中使用!= -1而不是== -1。

tags.filter上下文中什么是“標簽”? 我假設這是一些數組。 無論哪種情況,您的過濾器都在檢查標簽中的項目是否不等於filterTags數組。 當然,數組中的單個項目將不等於數組,因此它將始終返回true,因此不會過濾任何內容。

我認為您可能想要以下內容:

filteredTags = tags.filter(function(itm){return filterTags.indexOf(itm) !== -1});

您是在說這個數組嗎?

filterTags = ['churchevent', 'corporate'];
filteredTags = tags.filter(function(itm){return itm!== filterTags});

// Of note, you are creating tags just below this code. Should you move it up?
// Or rename tags => collectionTags???

// Either way, the filter function you are using is not doing what you expect.

tags.filter(function(itm){
  // itm will be whatever, I'm guessing a string of some sort like "churchevent"

  // Now you are trying to compare a string like "churchevent" to the 
  // array filterTags.

  // This is what is happening...

  return itm !== ['churchevent', 'corporate'];

  //  What you want to do is this in ES5

  return (filterTags.indexOf(itm) === -1);

  // or this in ES6

  return !filterTags.includes(itm);
  // Note the bang in front.
  // TRUE will put itm in the tags array, FALSE will not.

}

另外,請參考MDN中的過濾器功能。

過濾功能(MDN)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM