Issue
I am struggling to convert these html attributes I extracted from widget tag to an object so I can change some of its values. Following are the attributes.
widget="fixtures" competition="1" season="2020" match="123" template="cell" live="true" sport="hockey"
What I have tried is JSON.stringify and JSON.parse but nothing seems to be working.
This is what I have done, the ending result seems like object but when I try to access one of any key from it I get undefined.
htmlAttribs = htmlAttribs.replace(/[=]/g, ':');
htmlAttribs = htmlAttribs.replace(/[ ]/g, ', ');
htmlAttribs = `{${htmlAttribs}}`;
htmlAttribs = JSON.stringify(htmlAttribs);
htmlAttribs = JSON.parse(htmlAttribs);
console.log(htmlAttribs);
Solution
This worked for me.
htmlAttribs = htmlAttribs.replace(/[=]/g, ':');
htmlAttribs = htmlAttribs.replace(/[ ]/g, ', ');
const obj = eval('({' + htmlAttribs + '})');
One can access values using object.key_name
Answered By - Mohsin
Answer Checked By - - Katrina (ReactFix Volunteer)