Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. (() => {
  2. const tbody = [...document.querySelector("table").querySelectorAll("tr")]
  3. .map(tr =>
  4. [...tr.querySelectorAll("td")]
  5. .map(td => (
  6. td.querySelector("img") ?
  7. td.querySelector("a").href :
  8. td.textContent)
  9. )
  10. );
  11. const thead = tbody.shift();
  12. const data = tbody.map(td =>
  13. td.reduce(
  14. (prev, curVal, curIdx) => Object.assign(prev, { [thead[curIdx]]: curVal }),
  15. {}
  16. )
  17. );
  18. const pokemons = data.reduce((prev, cur) => Object.assign(prev, { [cur['Pic']]: cur['Pokémon'] }), { '-': null });
  19. const convertedData = data.map(datum => Object.assign(datum, {
  20. 'Evolves From': pokemons[datum['Evolves From']],
  21. 'Evolves Into': pokemons[datum['Evolves Into']],
  22. }, {})).map(datum => (
  23. delete datum['Pic'],
  24. datum['Candy Required'] = datum['Candy Required'] === '-' ? null : Number(datum['Candy Required']),
  25. datum['Type 1'] = /[^\/]+(?=-type-pokemon.html)/.exec(datum['Type 1']),
  26. datum['Type 1'] = datum['Type 1'] && datum['Type 1'][0],
  27. datum['Type 2'] = /[^\/]+(?=-type-pokemon.html)/.exec(datum['Type 2']),
  28. datum['Type 2'] = datum['Type 2'] && datum['Type 2'][0],
  29. datum
  30. ));
  31. console.log(JSON.stringify(convertedData));
  32. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement