简体   繁体   中英

Remove multiple elements from array

I'd like to remove multiple specific elements from my array before it displays. Here is the code I have but it results in none of the elements being displayed:

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

I'm supposing that, since you wrote "remove multiple specific elements" you want to REMOVE filterTags.

If that's the case then change this:

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>');

to this:

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>');

else just use != -1 instead of == -1 in the filter method.

What is "tags" in the context of tags.filter ? I'm assuming it is some array. In either case, your filter is checking that an item in tags is not equal to filterTags , an array. Of course a single item in an array won't be equal to an array, so this will always return true, thus not filtering anything.

I think you probably want something like:

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

Are you speaking about this array?

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.

}

Also, please reference the filter function in MDN.

Filter Function (MDN)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM