revision:
The Document.createNodeIterator() method returns a new NodeIterator object.
createNodeIterator(root)
createNodeIterator(root, whatToShow)
createNodeIterator(root, whatToShow, filter)
Parameters:
root : the root node at which to begin the NodeIterator's traversal.
whatToShow : optional. An optional "unsigned long" representing a bitmask created by combining the constant properties of "NodeFilter". It is a convenient way of filtering for certain types of node. It defaults to 0xFFFFFFFF representing the "SHOW_ALL constant".
const nodeIterator = document.createNodeIterator(
document.body,
NodeFilter.SHOW_ELEMENT,
(node) =>
node.nodeName.toLowerCase() === "p"
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT
);
const pars = [];
let currentNode;
while ((currentNode = nodeIterator.nextNode())) {
pars.push(currentNode);
}