Using an Angular application which inserts ‘ng-star-inserted‘ to every node element. I need to check if the target element class exists inside array
var footerElementClassList = [
'footer-links__link',
'social__link-icon',
'bottom__link',
];
const filtered = footerElementClassList.includes('bottom__link ng-star-inserted');
console.log(filtered);
Tried below options but no luck
2
Answers
As you privide the classes as a single string, separated by a space you first need to split them. This code does check for all classes.
There are two data-formats …
Since one has to use comparison approaches which fit the different formats, an array of string-based name values versus a string of space-separated name-values, one can come up with mainly two approaches …
split
the string of class-names and iterate its resulting array in order to look whether the class-list arrayincludes
every
of the split class-names which makes the approaches complexity quadratic (includes
nested insideevery
) …create a
RegExp
from thesplit
andsort
ed string of class-names which then getstest
ed against thesort
ed andjoin
ed string-version of the provided array of class-names. The complexity comes with the approach itself which needs to sort both participants and has to create a valid/working regex like e.g./bbottom__linkb.*?bng-star-insertedb/
on the fly …… working example code …