javascript - {li}之间有
问题描述
您好我正在尝试使用 DOM 显示一个数组。问题是我在 LI 元素之间得到了“,”。我怎样才能删除它?
const boresult = {
success: ["max-length", "no-amd", "prefer-arrow-function"],
failure: ["no-var", "var-on-top", "linebreak"],
skipped: ["id-blacklist", "no-dup-keys"]
};
function makeList(arr) {
const resultDisplayArray = [];
for (let i = 0; i < arr.length; i++) {
resultDisplayArray.push(`<li>${arr[i]}</li>`);
// resultDisplayArray.push('<li style="color: red">' + arr[i] + '</li>');
}
return resultDisplayArray;
}
// document.getElementById('ba').innerHTML = makeList(boresult.success);
const resultDisplayArray = makeList(boresult.success);
document.getElementById('ba').innerHTML = resultDisplayArray;
这是一个屏幕截图:
解决方案
You could join the array with an empty string.
If you take just the array and the environment expect a string, then the toString
method is called, which is basically the same as Array#join
, but it takes a default string of ,
for joining.
To prevent this, you need to take an own parameter.
function makeList(array) {
return array.map(v => `<li>${v}</li>`);
}
const boresult = { success: ["max-length", "no-amd", "prefer-arrow-function"], failure: ["no-var", "var-on-top", "linebreak"], skipped: ["id-blacklist", "no-dup-keys"] };
document.getElementById('ba').innerHTML = makeList(boresult.success).join('');
<ul id="ba"></ul>
推荐阅读
- octave - 如何在 GNU Octave 中向量化匿名函数?
- angular - 在 NgRx 效果中,ActivatedRoute 始终指向根
- laravel - 如何使用运行到页面的单个查询导入多个表数据
- rust - 如何在 Rust 中构造状态对象?
- python - 用 Beautiful-soup 抓取 python
- javascript - 未定义索引 - 尝试将 base64 编码图像发布到 PHP
- c++ - 如何创建模糊着色器
- python - 调整 Selenium 浏览器窗口的大小以模拟手机屏幕
- javascript - 如何绕过下面的脚本
- php - Laravel 7 使用 Passport 进行多重身份验证