有没有办法将 ul 中的 li 列表转换为数组?
Is there a way to convert a list of li's in a ul into an array?
我正在获取一些数据,返回结果如下:
> denialReasonContent: { denialReason: "<ul>\n <li>denial reason
> 1</li>\n <li>denial reason 2</li>\n</ul>" };
我正在尝试获取充满 li 的 ul 元素的数据并转换为如下所示的数组
var denialReasons = [{denialReason: denial reason 1}, {denialReason: denial reason 2}];
解析此数据并转换为新数组的最佳方法是什么?
如果您确定得到有效的 html 字符串作为响应,此代码可以提供帮助
let denialReasonContent = { denialReason: `<ul>\n <li>denial reason 1</li>\n <li>denial reason 2</li>\n</ul>` };
let div = document.createElement('div');
div.innerHTML = denialReasonContent.denialReason
let list = div.getElementsByTagName("li")
let data = [];
for(let i=0; i<list.length;i++) {
data.push({denialReason:list[i].innerText})
}
console.log(data)
// [{denialReason: denial reason 1}, {denialReason: denial reason 2}];
我正在获取一些数据,返回结果如下:
> denialReasonContent: { denialReason: "<ul>\n <li>denial reason
> 1</li>\n <li>denial reason 2</li>\n</ul>" };
我正在尝试获取充满 li 的 ul 元素的数据并转换为如下所示的数组
var denialReasons = [{denialReason: denial reason 1}, {denialReason: denial reason 2}];
解析此数据并转换为新数组的最佳方法是什么?
如果您确定得到有效的 html 字符串作为响应,此代码可以提供帮助
let denialReasonContent = { denialReason: `<ul>\n <li>denial reason 1</li>\n <li>denial reason 2</li>\n</ul>` };
let div = document.createElement('div');
div.innerHTML = denialReasonContent.denialReason
let list = div.getElementsByTagName("li")
let data = [];
for(let i=0; i<list.length;i++) {
data.push({denialReason:list[i].innerText})
}
console.log(data)
// [{denialReason: denial reason 1}, {denialReason: denial reason 2}];