2017-04-05 170 views
0

我想在显示数组之前从数组中删除多个特定元素。这里是代码我有,但它会导致没有的元素显示:从数组中删除多个元素

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

你定义'tags'你用它来过滤后,这是行不通的。另外,你的'tags.filter'函数不起作用,你需要检查'filterTags'数组中是否存在'itm',而不是它们是否相同。 – Adam

回答

1

我假设,因为你写了“删除多个特定元素”要删除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!

+0

使用== -1不做任何事情。使用!= -1删除所有标签。 – Capwiz

+0

您是否注意到在我的代码中,我在filterTags中检查了indexOf == -1?我只是没有改变操作员...更好看。 –

+0

我使用了您的整个代码,并且没有标签被过滤掉。没有影响 – Capwiz

0

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

我想你可能想是这样的:

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

他说他想删除一些标签。所以这意味着filterTags应该被删除,而不是验证。所以它应该是== -1 –

+0

我试过这个,它删除了所有的标签 – Capwiz

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)

+0

我试了下面,它只是删除所有的标签 'filterTags = ['wedding','corporate']; tags = $ .map(collectTags,createTag); filteredTags = tags.filter(function(itm){return filterTags.indexOf(itm)> -1}); (1).join(','))。join('
');' – Capwiz

+0

我改变了答案,从<> .concat(caption?[caption,link]:link).concat(filteredTags.slice ===我注意到你的问题不是平等的。 – SoEzPz