2017-04-05 278 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