I have an array of classes on element A and I would like to match those classes against an array, B. The classList
array will always be longer than filters
by a varying amount. For this reason, I believe that this is not a duplicate of this question.
I would like to do the following:
if( all elements of array B are present in array A ) {
Add this element to elsIn;
} else {
Add this element to elsOut;
}
I have tried following the answer in this SO Question, but it's not too helpful to me.
I know that jQuery offer a $.inArray
function, but this would mean that I would need multiple loops to achieve my outcome and would be inefficient.
I have also found this question and answer however the JS code matched only against one element in an array instead of all elements.
var elsOut = [],
elsIn = [],
filters = [],
classList;
$(document).on('click', '[data-filter]', function(e) {
if ($(this).hasClass('is-active')) {
filters.pop($(this).data('filter'));
} else {
filters.push($(this).data('filter'));
}
$('.trainer').each(function() {
classList = this.className.split(' ');
if (classList.contains(filters)) { // if all elements in "filters" match an element in "classList"
elsIn.push(this);
} else {
elsOut.push(this);
}
});
$(this).toggleClass('is-active');
$(elsOut).fadeOut();
$(elsIn).fadeIn();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>