Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从数组中删除多个元素_Javascript_Arrays_Filter_Fancybox_Caption - Fatal编程技术网

Javascript 从数组中删除多个元素

Javascript 从数组中删除多个元素,javascript,arrays,filter,fancybox,caption,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,

我想在数组显示之前从数组中删除多个特定元素。以下是我的代码,但它不会显示任何元素:

$('[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;   
});
$(“[data fancybox]”)。在('click',function()上{
var visibleLinks=$('.fancybox:visible');
$.fancybox.open(可访问链接{
//选项在这里
标题:函数(实例、项){
变量标题、链接、collectTags、tags、filterTags、filteredTags;
函数格式(tpl,绑定){
if(typeof binding!=“function”)返回格式(tpl,function(uz,name){
返回绑定[名称];
});
返回tpl。替换(/\$(\w+)/g,绑定);
}
caption=$(this.data('caption');
link=格式(“
查看更多图片”,项目); collectTags=$(this.parent().attr(“类”).split(“”); 函数createTag(it){ 返回格式(“”{ 地点:(它==“婚礼”|它==“音乐会”)?'http://example.com/gallery/#filter=.' : 'http://example.com/gallery/#filter=.', 它:它 }); } filterTags=['churchevent','corporate']; filteredTags=tags.filter(函数(itm){return itm!==filterTags}); 标记=$.map(collectTags,createTag); return[].concat(标题?[caption,link]:link).concat(filteredTags.slice(1).join(',')).join('
'); } },visibleLinks.index(this)); 返回false; });
标记的上下文中,“标记”是什么。过滤器
?我假设它是一个数组。在任何一种情况下,您的筛选器都会检查标记中的项是否不等于数组
filterTags
。当然,数组中的单个项并不等于数组,因此它将始终返回true,因此不会过滤任何内容

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

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

我假设,既然你写了“删除多个特定元素”,你就想删除FilterTag

如果是这种情况,则更改此项:

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'];
filteredTags=tags.filter(函数(itm){return itm!==filterTags});
标记=$.map(collectTags,createTag);
return[].concat(标题?[caption,link]:link).concat(filteredTags.slice(1).join(',')).join('
');
为此:

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>');
filterTags=['churchevent','corporate'];
标记=$.map(collectTags,createTag);
filteredTags=标签。过滤器((项目)=>{
对于(filterTags中的标记),如果(item.indexOf(filterTags[tag])!=-1)返回false;
返回true;
});
return[].concat(标题?[caption,link]:link).concat(filteredTags.slice(1).join(',')).join('
');

否则就用吧!=-1而不是filter方法中的==-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中的筛选函数


在使用它进行筛选之后,您正在定义
标记
,这是行不通的。此外,您的
标记.filter
功能不起作用,您需要检查
filterTags
数组中是否存在
itm
,而不是它们是否相同。他说他想删除一些标记。这意味着过滤器标签应该被移除,而不是验证。所以它应该是==-1使用==-1什么都不做。使用!=-1将删除所有标记。您是否注意到,在我的代码中,我在filterTags中选中了indexOf==-1?我没有改变接线员只是。。。仔细看看。我使用了你的全部代码,没有过滤掉任何标记。我的坏消息没有影响。有个打字错误<代码>(itm)=>filterTags.indexOf(项目)应为
(项目)=>filterTags.indexOf(项目)
。更正。我现在使用的是
filteredTags=tags.filter((项)=>filterTags.indexOf(项)=-1)仍然没有过滤出任何数组项。我尝试了以下方法,它只是删除了所有标记
filterTags=['widding','corporate'];标记=$.map(collectTags,createTag);filteredTags=tags.filter(函数(itm){return filterttags.indexOf(itm)>-1});return[].concat(标题?[caption,link]:link).concat(filteredTags.slice(1).join(',')).join('
')我将答案从>==改为>==因为我注意到你的问题不希望相等。