繁体   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