Skip to content

Instantly share code, notes, and snippets.

@holloway
Last active September 9, 2016 00:55
Show Gist options
  • Save holloway/18a8836922f8adf59daa3f3be9597f2c to your computer and use it in GitHub Desktop.
Save holloway/18a8836922f8adf59daa3f3be9597f2c to your computer and use it in GitHub Desktop.
export function querySelectArray(selector, el = document) {
// Usage: fn(selector, document.body)
// Returns [el1, el2, ...]
return Array.prototype.slice.call(el.querySelectorAll(selector));
}
export function querySelectArrayByElementArray(selector, els, removeDuplicates=true){
// Usage: fn(selector, [document.body, document.head])
// Returns [elementFromBody1, elementFromBody2, elementFromHead1, ...]
let arr = Array.prototype.concat.apply([], Array.prototype.slice.call(els).map( (el) => querySelectArray(selector, el) ));
if(removeDuplicates) arr = arr.filter( (el, index, self) => index === self.indexOf(el) );
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment