Guest User

Untitled

a guest
Nov 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. // Pure JavaScript
  2. function selectToCollection(selector, asJson) {
  3. var options = Array.from(document.querySelector(selector).querySelectorAll('option'));
  4. var collection = options.reduce((prev, curr) => {
  5. return [...prev, { label: curr.label, value: curr.value}];
  6. }, []);
  7. return (asJson === true) ? JSON.stringify(collection) : collection;
  8. }
  9.  
  10.  
  11. // With jQuery
  12. function selectToCollection(selector, asJson) {
  13. var collection = $(selector).find('option').toArray().reduce((prev, curr) => {
  14. return [...prev, { label: curr.label, value: curr.value}];
  15. }, []);
  16. return (asJson === true) ? JSON.stringify(collection) : collection;
  17. }
Add Comment
Please, Sign In to add comment