I have an forEach, for a document with querySelectorAll.
In there, I have a name and some more variables. Is it possible to Sort the forEach Loop and check if there are words, matching with my array to show these ones first?
So i have a list like that:
['vollautomat', 'fernseher']
And i will that items with "name" = vollautomat or fernseher will showed first. Than the others.
document.querySelectorAll(".vvp-item-tile").forEach(function (element, index) {
var name = element.querySelector('.a-link-normal').innerText;
console.log(name)
});
How is it possible to sort the forEach that these will showed first?
I tried to find out with searching but all I found was to short arrays and not a forEach loop with document.querySelecterAll
2
Answers
I would try to convert the
NodeList
returned byquerySelectorAll
into anarray
. Then would sort the array based on the criteria (for instance, if the name matches an item in your list). Finally sort the array.Since there’s no HTML I’m assuming that the selector:
".vvp-item-tile"
is unnecessary and".a-link-normal"
is sufficient. Run the function from the example below with the following parameters:Details are commented in the example.