I want to put all attributes in a Html element into an array: like i have a jQuery Object, whichs html looks like this:
Attributes to Object conversion
*Requires: lodash
function getAttributes(element, parseJson=false){
let results = {}
for (let i = 0, n = element.attributes.length; i < n; i++){
let key = element.attributes[i].nodeName.replace('-', '.')
let value = element.attributes[i].nodeValue
if(parseJson){
try{
if(_.isString(value))
value = JSON.parse(value)
} catch(e) {}
}
_.set(results, key, value)
}
return results
}
This will convert all html attributes to a nested object
Example HTML:
Result: {custom:{nested:{path1:"value1",path2:"value2"}}}
If parseJson is set to true json values will be converted to objects