从数组中删除多个元素

时间:2017-04-05 22:31:37

标签: javascript arrays filter fancybox caption

我希望在显示之前从我的数组中删除多个特定元素。这是我的代码,但它没有显示任何元素:

$('[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;   
});

3 个答案:

答案 0 :(得分:1)

我想,因为你写了&#34;删除多个特定元素&#34;你想删除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。

答案 1 :(得分:0)

什么是&#34;标签&#34;在tags.filter的背景下?我假设它是一些数组。在任何一种情况下,您的过滤器都会检查标记中的项目是否等于filterTags数组。当然,数组中的单个项目不等于数组,因此这将始终返回true,因此不会过滤任何内容。

我想你可能想要这样的东西:

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

答案 2 :(得分:0)

你是在谈论这个阵列吗?

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中的过滤功能。

Filter Function (MDN)