Created
October 7, 2017 01:45
-
-
Save NazoTex/43a513ea52dd04269ea279369d32fa3d to your computer and use it in GitHub Desktop.
Build an Object from a table by cell classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// orginally used for scraping a specification table. Rename function as you like. | |
// WARNING: This is a mix of es5 and es6. Take care with older browsers! | |
function getSpecs(col1, col2) { | |
var keys = []; | |
var val = []; | |
var result = {}; | |
// + build the arrays | |
for(var i=0; i < document.querySelectorAll(col1).length; i++) { | |
// Everything passed .nodeValue is to clean up any spaces. You don't want spaces in your keys if you can help it | |
keys[i] = document.querySelectorAll(col1)[i].firstChild.nodeValue.trim().toLowerCase().split(' ').join('_'); | |
} | |
for(var i=0; i < document.querySelectorAll(col2).length; i++) { | |
val[i] = document.querySelectorAll(col2)[i].firstChild.nodeValue.trim(); | |
} | |
// + Now build the obj. | |
// WARNING: If you need redundant Key names this will reduce them to the last version(i.g, item:thing,item:thing2 = item:thing2)! | |
keys.forEach((key, i) => result[key] = val[i]); | |
// + Get the resulting obj! | |
return result | |
} | |
// Created a variable to get to key:value pairs more easily. You can use specList.keyname to get the value. | |
var specList = getSpecs('.spec-name','.spec-value'); | |
console.log(specList); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment