I have an html string that contains multiple <p>
tags. WIthin each <p>
tag there is a word and its definition.
let data = "<p><strong>Word 1:</strong> Definition of word 1</p><p><strong>Word 2:</strong> Definition of word 2</p>"
My goal is to convert this html string into an array of objects that looks like below:
[
{"word": "Word 1", "definition": "Definition of word 1"},
{"word": "Word 2", "definition": "Definition of word 2"}
]
I am doing it as follows:
var parser = new DOMParser();
var parsedHtml = parser.parseFromString(data, "text/html");
let pTags = parsedHtml.getElementsByTagName("p");
let vocab = []
pTags.forEach(function(item){
// This is where I need help to split and convert item into object
vocab.push(item.innerHTML)
});
As you can see the comment in the above code, that is where I'm stuck. Any help is appreciated.