There doesn't appear to be a way to get the results directly from `document.evaluate()`.
Taking the concept from [this answer](https://stackoverflow.com/a/42600459/1575353), one could get [a snapshot](https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Snapshots) result, create an array [filled](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill) with dummy values (e.g. `0`s) and then [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) calls to [snapshotItem()](https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMXPathResult#snapshotItem()).
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var query = document.evaluate("//*[@*[starts-with(name(), 'temp')]]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var results = Array(query.snapshotLength).fill(0).map(function(element, index) {
return query.snapshotItem(index);
});
console.log(results);
<!-- language: lang-html -->
<div temp-callback="1" temp-post-callback="4"></div>
<div temp-callback="2"></div>
<div></div>
<div temp-callback="8" temp-post-callback="7"></div>
<!-- end snippet -->
While less of a functional programming approach, one could also just use a _for_ loop:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var query = document.evaluate("//*[@*[starts-with(name(), 'temp')]]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i=0, results = []; i< query.snapshotLength; results.push(query.snapshotItem(i++)));
console.log(results);
<!-- language: lang-html -->
<div temp-callback="1" temp-post-callback="4"></div>
<div temp-callback="2"></div>
<div></div>
<div temp-callback="8" temp-post-callback="7"></div>
<!-- end snippet -->
Additionally, given the last example of [_Equivalent constructs_](https://en.wikipedia.org/wiki/While_loop#Equivalent_constructs), the _while_ loop could be rewritten as a _for_ loop as well:
for ( ; condition; ) {
statements;
}
So the original code could be written as such:
var nodes = [];
for(var node = result.iterateNext(); node; node = result.iterateNext()) {
nodes.push(node);
}
See this demonstrated in the snippet below:
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
var result = document.evaluate("//*[@*[starts-with(name(), 'temp')]]", document, null, XPathResult.ANY_TYPE, null);
var nodes = [];
for(var node = result.iterateNext(); node; node = result.iterateNext()) {
nodes.push(node);
}
console.log(nodes);
<!-- language: lang-html -->
<div temp-callback="1" temp-post-callback="4"></div>
<div temp-callback="2"></div>
<div></div>
<div temp-callback="8" temp-post-callback="7"></div>
<!-- end snippet -->
###Performance Comparison
Compare the four approaches with [this jsperf example](https://jsperf.com/pushingnodes). In Chrome, Firefox, and MS Edge it reports that the original code is the fastest.