JaSon JaSon - 2 months ago 8
jQuery Question

Check all matches with combined string + array

I'm trying to hide some unique links from a site's product listing page.
Those links' href attributes endings are set in my "hrefs" array.
I'd like to include only the endings (instead of the whole url-s), since the part before them are always the same.

The links which I want to hide from the site (in this case) are:

https://www.example.com/products/details/4123
https://www.example.com/products/details/4124
https://www.example.com/products/details/4130


My script is the next, the problem is it is only hiding the first match ( https://www.example.com/products/details/4123 ).

var hrefs = [ "4123", "4124", "4130", ];
var fullhref = ("https://www.example.com/products/details/") + (hrefs);
$("[href]").filter(function(){
return fullhref.indexOf(this.href) >-1;
}).parent().hide();


So it can combine the first part of the url and the last part from the array and remove that link, but only the first one.
I need some help how could the code check all of the given url endings (and hide all)?

Answer

you can modify the array with a loop then use it to filter the selection.

var hrefs = [ "4123", "4124", "4130", ];

for(var i = 0; i< hrefs.length; i++){
hrefs[i] = "https://www.example.com/products/details/" + hrefs[i];
}