我有一个包含多个<p>
标签的html字符串。每个<p>
标签中都有一个单词及其定义。
let data = "<p><strong>Word 1:</strong> Definition of word 1</p><p><strong>Word 2:</strong> Definition of word 2</p>"
我的目标是将此html字符串转换为如下所示的对象数组:
[
{"word": "Word 1", "definition": "Definition of word 1"},
{"word": "Word 2", "definition": "Definition of word 2"}
]
我正在这样做,如下:
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)
});
您可以在上面的代码中看到注释,这就是我遇到的问题。任何帮助表示赞赏。
答案 0 :(得分:3)
使用textContent
从元素中获取文本。这个词在strong
子元素中,定义是文本的其余部分。
var parser = new DomParser();
var parsedHtml = parser.parseFromString(data, "text/html");
let pTags = parsedHtml.getElementsByTagName("p");
let vocab = []
pTags.forEach(function(item){
let word = item.getElementsByTagName("strong")[0].textContent.trim();
let allText = item.textContent;
let definition = allText.replace(word, "").trim();
vocab.push({word: word, definition: definition})
});
答案 1 :(得分:0)
有点特殊,但可以。
const data = "<p><strong>Word 1:</strong> Definition of word 1</p><p><strong>Word 2:</strong> Definition of word 2</p>";
const parsedData = [
{
"word1": data.split('<strong>')[1].split('</strong>')[0].trim(),
"definition": data.split('</strong>')[1].split('</p>')[0].trim()
},
{
"word2": data.split('</p>')[1].split('<strong>')[1].split('</strong>')[0].trim(),
"definition": data.split('</p>')[1].split('</strong>')[1].split('</p>')[0].trim()
}
]
console.log(parsedData);
答案 2 :(得分:0)
您应该修复:
DOMParser
,而不是DomParser
pTags
无法使用.forEach()
,请使用for
循环我为您解决的问题的解决方案:
let data = "<p><strong>Word 1:</strong> Definition of word 1</p><p><strong>Word 2:</strong> Definition of word 2</p>"
var parser = new DOMParser();
var parsedHtml = parser.parseFromString(data, "text/html");
let pTags = parsedHtml.getElementsByTagName("p");
let vocab = [];
for (let p of pTags) {
const word = p.getElementsByTagName('strong')[0].innerHTML.replace(':', '').trim();
const definition = p.innerHTML.replace(/<strong>.*<\/strong>/, '').trim();
vocab.push( { word, definition } )
}
console.log(vocab);